QPixmap fails to load using resource - qt

I am trying to write a library with a custom Qt icon-text label type object in it. However the icon never displays although the widget is shown (as tested by replacing the pixmap with plain text).
My CMakeLists.txt:
...
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
COMMAND rcc -no-compress ${CMAKE_CURRENT_SOURCE_DIR}/src/qt/configmgr.qrc -name configmgr -o ${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
DEPENDS src/qt/configmgr.qrc src/qt/info.png
)
...
add_library(ConfigMgr STATIC
...
src/qt/section_header.cpp
${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
)
...
My configmgr.qrc:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>info.png</file>
</qresource>
</RCC>
This produces a "resources.cpp" That looks like this:
static const unsigned char qt_resource_data[] = {
0x0,0x0,0x1,0x76,
0x89,
0x50,0x4e,0x47,0xd,0xa,0x1a,0xa,0x0,0x0,0x0,0xd,0x49,0x48,0x44,0x52,0x0,
...
0x44,0xae,0x42,0x60,0x82,
};
static const unsigned char qt_resource_name[] = {
// info.png
0x0,0x8,
0x4,0xd2,0x59,0x47,
0x0,0x69,
0x0,0x6e,0x0,0x66,0x0,0x6f,0x0,0x2e,0x0,0x70,0x0,0x6e,0x0,0x67,
};
static const unsigned char qt_resource_struct[] = {
// :
0x0,0x0,0x0,0x0,0x0,0x2,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x1,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
// :/info.png
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x0,
0x0,0x0,0x1,0x78,0x7d,0xe0,0x40,0x12,
};
...
Build output includes resources.cpp.o and that should be getting linked into the library.
My class header:
class SectionHeader : public QWidget
{
Q_OBJECT
public:
SectionHeader(const std::string &text, const std::string &info);
virtual ~SectionHeader();
private:
std::string m_info;
QPixmap m_icon;
};
My class constructor:
SectionHeader::SectionHeader(const std::string &text, const std::string &info)
: m_info{info},
m_icon(":/info.png")
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label;
if (!m_info.empty()) {
label = new QLabel;
label->setPixmap(m_icon);
layout->addWidget(label);
}
label = new QLabel(text.c_str());
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet("font-weight: bold; font-family: Calibre; font-size: 10pt");
layout->addWidget(label);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
}
I have also tried calling load() on a QPixmap instance and it returns false.
Everything I have read, and similar code I have written before which is very similar to this, tells me that this should work. Why doesn't it?

The problem here is that I am trying to write a library.
I solved the problem by explicitly initialising the resources.
Find the generated CPP file for the resources. In my case it was resources.cpp.
Identify the name of the initialisation and cleanup functions. In my case they are:
int qInitResources_configmgr();
int qCleanupResources_configmgr();
Somewhere suitable, such as in the CPP file for your library's main class, locally declare these two functions.
Call the two functions in the appropriate place. E.g. main class constructor and destructor respectively.

Related

QLineEdit should accept hexadecimal values ranging from [0 - FFFFF]

I have a requirement where I want my QLineEdit should accept hexadecimal values ranging from [0 - FFFFF]. can someone help me out with this?
I have tried the below code, but it holds good for only 1 char display.
I don't see any code in your post, but what you're looking for is a validator, derived from the QValidator class. Create a sub-class, say HexValidator, and implement the "validate" method. You check the input string for the allowed characters and range and return the appropriate state.
You then assign the validator to the QLineEdit using the QLineEdit::setValidator method. Note that the QLineEdit doesn't take ownership of the validator, so you need to make sure you delete it separately or give it a parent so that it gets cleaned up when the parent is deleted. You can create a single validator and assign it to multiple fields if needed.
I was wrong in my comment about Qt docs having a Hex spin box example... so I found one in my archive instead. It could be more flexible, but OTOH it's very simple and short. I know this isn't a QLineEdit but maybe it'll help anyway. The QValidator used here could be used in a line edit also.
#ifndef _HEXSPINBOX_H_
#define _HEXSPINBOX_H_
#include <QSpinBox>
#include <QRegularExpressionValidator>
// NOTE: Since QSpinBox uses int as the storage type, the effective editing range
// is +/- 0x7FFF FFFF, so it can't handle a full unsigned int.
// QDoubleSpinBox would be a more suitable base class if a wider range is needed.
class HexSpinBox : public QSpinBox
{
Q_OBJECT
public:
HexSpinBox(QWidget *parent = nullptr,
bool showPrefix = false,
const QString &format = QStringLiteral("%x")) :
QSpinBox(parent),
format(format)
{
// Validates hex strings up to 8 chars with or w/out leading "0x" prefix.
// For arbitrary prefix/suffix, the regex could be built dynamically
// in validate(), or override setPrefix()/setSuffix() methods.
const QRegularExpression rx("(?:0[xX])?[0-9A-Fa-f]{1,8}");
validator = new QRegularExpressionValidator(rx, this);
setShowPrefix(showPrefix);
}
public slots:
void setShowPrefix(bool show)
{
if (show)
setPrefix(QStringLiteral("0x"));
else
setPrefix(QString());
}
void setFormat(const QString &text)
{
format = text;
lineEdit()->setText(textFromValue(value()));
}
protected:
QValidator::State validate(QString &text, int &pos) const override
{
return validator->validate(text, pos);
}
int valueFromText(const QString &text) const override
{
return text.toInt(0, 16);
}
QString textFromValue(int value) const override
{
return QString().sprintf(qPrintable(format), value);
}
private:
QRegularExpressionValidator *validator;
QString format;
};
#endif // _HEXSPINBOX_H_
Example:
#include "HexSpinBox.h"
#include <QApplication>
#include <QBoxLayout>
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog d;
d.setLayout(new QVBoxLayout);
HexSpinBox* sb = new HexSpinBox(&d);
sb->setMaximum(0xFFFFF);
sb->setValue(0x0A234);
d.layout()->addWidget(sb);
HexSpinBox* sb2 = new HexSpinBox(&d, true, QStringLiteral("%05X"));
sb2->setMaximum(0xFFFFF);
sb2->setValue(0x0A234);
d.layout()->addWidget(sb2);
return d.exec();
}

Setting QRegExp to QML TextField validator disables Textfield

I want to create a list of regexes to use in my application as singleton so I don't have to write it everywhere , I tried creating qml QtObject but it's not working in qml cause there is no RegExp object in qml and property var x = /regex/ not working as regex its a string.
I created a cpp class to put my regexes into it:
#ifndef UVALIDATORS_H
#define UVALIDATORS_H
#include <QObject>
#include <QRegExp>
#include <QQmlEngine>
#include <QJSEngine>
class UValidators : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(UValidators)
Q_PROPERTY(QRegExp time READ time )
public:
static QObject* instance(QQmlEngine *engine,QJSEngine *scriptEngine){
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return new UValidators;
};
explicit UValidators(QObject *parent = nullptr);
QRegExp time() const;
signals:
public slots:
};
#endif // UVALIDATORS_H
for cpp :
#include "uvalidators.h"
UValidators::UValidators(QObject *parent) : QObject(parent)
{
}
QRegExp UValidators::time() const{
QRegExp time;
time.setPattern("/^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/");
return time;
}
And I register it using :
qmlRegisterSingletonType<UValidators>("U",1,0,"Validators",&UValidators::instance);
And I use it like this :
TextField{
validator:RegExpValidator{
regExp:U.Validators.time
}
}
Everything is fine application runs but textfield wont let me write anything.
Is there any way to create regexp list in qml or cpp?
If your regex expressions stay at QML level you can simply store them in a shared js file:
// Regex.js
var time = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/;
Usable in any QML file:
import "Regex.js" as Regex
TextField {
validator: RegExpValidator {
regExp: Regex.time
}
}
problem is with regex /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/ if you want to set pattern to QRegExp you should drop / from start and end of regex. any way if you know a better solution to this regex issue in qml without cpp let me know!
time.setPattern("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$");

Instantiate QWidget derived classes in a QObject derived class

For some reason the Qt compiler doesn't compile if you try to pass a QObject derived class as the rparent to a QWidget derived class.
What is the correct way to provide a parent to QWidget derived classes in a QObject derived class? I'm thinking of the following solutions:
Use a smart pointer with the QWidget classes, instead of giving the object a parent.
Derive from QWidget instead of QObject (Sounds wrong to me, since the class isn't a widget).
Pass a QWidget instance to the QObject derviced class which already has a parent, as I've tried to demonstrate in the example below:
#include <QApplication>
#include <QtWidgets>
class ErrorMsgDialog;
class Controller;
class MainWindow;
// This is the QWidget class used in the QObject derived class (DataReadController)
class ErrorMsgDialog : public QDialog
{
Q_OBJECT
public:
explicit ErrorMsgDialog(QWidget *parent = nullptr)
: QDialog(parent)
{
errorLbl = new QLabel("An unknown read error occured!");
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(errorLbl);
setLayout(layout);
setWindowTitle("Error!");
setGeometry(250, 250, 250, 100);
}
~ErrorMsgDialog() { qDebug() << "~ErrorMsgDialog() destructed"; }
private:
QLabel* errorLbl;
};
// QObject derived class - I want to instatiate Qwidget derived classes here, with this class as parent
class DataReadController
: public QObject
{
Q_OBJECT
public:
DataReadController(QWidget* pw, QObject *parent = nullptr)
: QObject(parent)
{
m_errorMsgDialog = new ErrorMsgDialog(pw);
//m_errorMsgDialog = QSharedPointer<ErrorMsgDialog>(m_errorMsgDialog);
//m_dataReader = new DataReader(this); m_dataReader->moveToThread(m_dataReaderThread); connect(....etc
//simulate that DataReader emits an error msg
QTimer::singleShot(2000, [&]() {
onErrorTriggered();
});
}
public slots:
void onNewDataArrived() {}
// called if reader emits an error message
void onErrorTriggered() { m_errorMsgDialog->show(); }
private:
ErrorMsgDialog* m_errorMsgDialog;
//QSharedPointer<ErrorMsgDialog> m_errorMsgDialog;
//DataReader* m_dataReader;// DataReader is not shown here, it would be moved to a different thread and provide some data
};
// MainWindow
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr)
: QMainWindow(parent)
{
parentWidget = new QWidget(this);
m_dataReadController = new DataReadController(parentWidget, this);
setGeometry(200, 200, 640, 480);
//Close after 5 seconds.
QTimer::singleShot(5000, [&]() {
close();
});
}
private:
QWidget* parentWidget; // QWidget to pass to OBject derive class for parenting QWidgets
DataReadController* m_dataReadController;
};
// Main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
This test crashes if I use QSharedPointer with ErrorMsgDialog.
Any suggestions on the way to do this? Maybe none of my suggested solutions is the best practise?
Correct storage and life-time management of dynamically created objects is not quite easy in C++. After a long time of struggling, I started to prefer certain techniques which I still use quite successful:
local variables (storage and life-time managed by scopes)
non-pointer member variables
smart pointers with clear ownership (shared pointer) or non-ownership (weak pointer).
With this, I banned raw pointers from my sources nearly completely resulting in much maintenance friendlier code and less annoying debugging.
Of course, when external libraries (like e.g. widget sets) come into play, then I am bound to their APIs.
Concerning gtkmm 2.4, the above techniques worked quite good as well. gtkmm provides
smart pointers for sharable objects
some kind of ownership for widgets concerning child widgets.
When I switched to Qt, I saw all the news and raw pointers in the tutorial samples which made me afraid a bit. After some experiments, I came to the conclusion that I will be able to write ful-featured Qt applications similar like I did before in gtkmm – without nearly any need of new by (again) defining widgets as local variables (e.g. in main()) or member variables of other classes derived (directly or indirectly) from QWidget.
Beside of this, over the time, I realized the general Qt concept of Object Trees & Ownership but I must admit that I rarely rely on this in daily business.
Concerning the specific problem of OP:
A QWidget is derived of QObject. Hence, the usual ownership principle of QObjects apply. Additionally, a QWidget expects another QWidget as parent. A QWidget may be parent of any QObjects but not vice versa.
Hence, I would suggest the following ownership:
MainWindow is parent of DataReadController
MainWindow is parent of ErrorMsgDialog (which is created in DataReadController).
DataReadController stores a pointer to ErrorMsgDialog as raw pointer. (I believe the ownership provided by a QSharedPointer would collide with the ownership of MainWindow.)
(As already described above, in my own programming, I would try to prevent pointers at all and use (non-pointer) member variables instead. However, IMHO that's a matter of style and personal preference.)
The modified sample of OP testQParentship.cc:
#include <QtWidgets>
class Label: public QLabel {
private:
const QString _name;
public:
Label(const QString &name, const QString &text):
QLabel(text),
_name(name)
{ }
virtual ~Label()
{
qDebug() << _name + ".~Label()";
}
};
class HBoxLayout: public QHBoxLayout {
private:
const QString _name;
public:
HBoxLayout(const QString &name):
QHBoxLayout(),
_name(name)
{ }
virtual ~HBoxLayout()
{
qDebug() << _name + ".~HBoxLayout()";
}
};
class ErrorMsgDlg: public QDialog {
private:
const QString _name;
public:
ErrorMsgDlg(const QString &name, QWidget *pQParent):
QDialog(pQParent),
_name(name)
{
QHBoxLayout *pQBox = new HBoxLayout("HBoxLayout");
pQBox->addWidget(
new Label("Label", "An unknown read error occured!"));
setLayout(pQBox);
setWindowTitle("Error!");
}
virtual ~ErrorMsgDlg()
{
qDebug() << _name + ".~ErrorMsgDlg()";
}
};
class DataReadCtrl: public QObject {
private:
const QString _name;
ErrorMsgDlg *const _pDlgErrorMsg;
public:
DataReadCtrl(const QString &name, QWidget *pQParent):
QObject(pQParent),
_name(name),
_pDlgErrorMsg(
new ErrorMsgDlg(name + "._pDlgErrorMsg", pQParent))
{
//simulate that DataReader emits an error msg
QTimer::singleShot(2000, [&]() {
onErrorTriggered();
});
}
virtual ~DataReadCtrl()
{
qDebug() << _name + ".~DataReadCtrl()";
}
void onErrorTriggered()
{
_pDlgErrorMsg->show();
}
};
class MainWindow: public QMainWindow {
private:
const QString _name;
DataReadCtrl *_pCtrlReadData;
public:
MainWindow(const char *name):
QMainWindow(),
_name(name),
_pCtrlReadData(nullptr)
{
_pCtrlReadData
= new DataReadCtrl(_name + "._pCtrlReadData", this);
//Close after 5 seconds.
QTimer::singleShot(5000, [&]() {
qDebug() << _name + ".close()";
close();
});
}
virtual ~MainWindow()
{
qDebug() << _name + ".~MainWindow()";
}
};
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
MainWindow winMain("winMain");
winMain.show();
// runtime loop
return app.exec();
}
and a minimal Qt project file testQParentship.pro:
SOURCES = testQParentship.cc
QT += widgets
Compiled and tested in cygwin64 on Windows 10:
$ qmake-qt5 testQParentship.pro
$ make && ./testQParentship
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQParentship.o testQParentship.cc
g++ -o testQParentship.exe testQParentship.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
Qt Version: 5.9.4
After the QTimer::singleshot() in MainWindow is due, the main window is closed. The output of diagnostics illustrates that the object tree is destructed properly (instead of "thrown" away when OS releases process memory).
"winMain.close()"
"winMain.~MainWindow()"
"winMain._pCtrlReadData.~DataReadCtrl()"
"winMain._pCtrlReadData._pDlgErrorMsg.~ErrorMsgDlg()"
"HBoxLayout.~HBoxLayout()"
"Label.~Label()"

How to read widgets from the .ui file in qt5?

I am trying to get the list of widgets from a .ui files.
So here is a bit of code:
QUiLoader loader;
QFile file(fname);
file.open(QFile::ReadOnly);
QWidget *myWidget = loader.load(&file, this);
file.close();
QStringList avlb_wd = loader.availableWidgets();
QMessageBox msg;
foreach (const QString &str, avlb_wd)
{
msg.setText(str);
msg.exec();
}
But as I can see, availableWidgets() gives me all the widgets, not the ones that are in .ui file.
How can I achieve it?
Thanks forward.
Make a subclass of QUiLoader, and reimplement createWidget, createLayout and createAction (there's also createActionGroup, but it's not really supported any more, unless you manually edit the ui file).
These functions are called every time a new widget, layout or action is created by the ui loader. So just call the base-class implementation to get the created object, and then you can gather whatever information you like, before returning it.
UPDATE:
So the basic QUiLoader subclass would look like this (add other overloads as required):
class UiLoader : public QUiLoader
{
Q_OBJECT
public:
UiLoader(QObject *parent = 0) : QUiLoader(parent) { }
virtual QWidget* createWidget(const QString &className,
QWidget *parent = 0, const QString &name = QString())
{
QWidget* widget = QUiLoader::createWidget(className, parent, name);
// do stuff with className, name, widget, etc
return widget;
}
};

Qt Dynamic translation of dialog windows

I am creating a Qt application and I added dynamic translation (I followed the example at http://www.qtcentre.org/wiki/index.php?title=Dynamic_translation_in_Qt4_applications) with a QCombobox which lists different languages. It works well but the problem is that I don't see how to translate dynamically the text in the dialog windows (for example YES and NO buttons).
In the main.cpp, before executing the app, I have :
QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
a.installTranslator(&qtTranslator);
which translate the dialog Windows in the user system language but I would like to do it dynamically like the rest of my app.
Here are the code of the example :
application.h :
#ifndef APPLICATION_H
#include <QApplication>
#include <QHash>
#include <QStringList>
class QDir;
class QTranslator;
typedef QHash<QString, QTranslator*> Translators;
class Application : public QApplication
{
Q_OBJECT
public:
explicit Application(int& argc, char* argv[]);
~Application();
static void loadTranslations(const QString& dir);
static void loadTranslations(const QDir& dir);
static const QStringList availableLanguages();
public slots:
static void setLanguage(const QString& locale);
private:
static QTranslator* current;
static Translators translators;
//static QTranslator* qtTranslator;//test to translate dialog windows
};
#endif // APPLICATION_H
application.cpp :
#include <QDir>
#include <QFileInfo>
#include <QTranslator>
#include <QLibraryInfo>
#include "application.h"
QTranslator* Application::current = 0;
//QTranslator* Application::qtTranslator = 0;//test to translate dialog windows
Translators Application::translators;
Application::Application(int& argc, char* argv[])
: QApplication(argc, argv)
{
}
Application::~Application()
{
}
void Application::loadTranslations(const QString& dir)
{
loadTranslations(QDir(dir));
QString locale = QLocale::system().name().section('_', 0, 0);
QString language=locale+ "_" + locale;
if(!QFile::exists(":Localization/Localization/"+language+".qm"))//if system language is not available, load english version
setLanguage("en_en");
else
setLanguage(language);
}
void Application::loadTranslations(const QDir& dir)
{
// <language>_<country>.qm
QString filter = "*_*.qm";
QDir::Filters filters = QDir::Files | QDir::Readable;
QDir::SortFlags sort = QDir::Name;
QFileInfoList entries = dir.entryInfoList(QStringList() << filter, filters, sort);
foreach (QFileInfo file, entries)
{
// pick country and language out of the file name
QStringList parts = file.baseName().split("_");
QString language = parts.at(parts.count() - 2);
QString country = parts.at(parts.count() - 1);
// construct and load translator
QTranslator* translator = new QTranslator(instance());
if (translator->load(file.absoluteFilePath()))
{
QString locale = language + "_" + country;
translators.insert(locale, translator);
}
}
}
const QStringList Application::availableLanguages()
{
// the content won't get copied thanks to implicit sharing and constness
return QStringList(translators.keys());
}
void Application::setLanguage(const QString& locale)
{
//test to translate dialog windows
/*
QTranslator qtTranslator;
QString qTLocale=locale.mid(0,2);
qtTranslator->load("qt_"+ qTLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
installTranslator(qtTranslator);
//*/
// remove previous
if (current)
{
removeTranslator(current);
}
// install new
current = translators.value(locale, 0);
if (current)
{
installTranslator(current);
}
}
I added the lines commented with "//test to translate dialog Windows" to try the dynamic translation of the dialog Windows but it doesn't work (no error at compilation but the application isn't launched with error message "the program stopped suddenly", I am on Qt Creator). Thanks!
So I finally got this to work after having the same problems. There are two things which were wrong in my case:
Name of the qt translation file:
QTranslator qtTranslator;
qtTranslator.load("qt_de"); // worked in older qt versions
qtTranslator.load("qtbase_de"); // works for qt5.2
a.installTranslator(&qtTranslator);
Have the correct parent for the QMessageBox. This is obvious after you think about it but pretty easy to miss.
QMessageBox::information(someChildOfMainWindow, ...);
For the latter, if you happen to be in a class which is a QObject but not a QWidget you can also use the following code to access your MainWindow from anywhere:
QMainWindow* mw = 0;
foreach(QWidget* widget, QApplication::topLevelWidgets()) {
if(widget->objectName() == "<your-main-window-class-name-here>") {
mw = qobject_cast<QMainWindow>(widget);
}
}
Ok Sébastian Lange, so finally I created the box and didn't use the static ones (
QMessageBox::question(..) for example)
QMessageBox quitMessageBox;
quitMessageBox.setWindowTitle(tr("Quit"));
quitMessageBox.setWindowIcon(QIcon("myIcon.jpg"));
quitMessageBox.setIcon(QMessageBox::Question);
quitMessageBox.setText(tr("Quit the application?"));
quitMessageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
quitMessageBox.setDefaultButton(QMessageBox::No);
quitMessageBox.button(QMessageBox::Yes)->setText(tr("Yes"));
quitMessageBox.button(QMessageBox::No)->setText(tr("No"));
And then
quitMessageBox.exec();
Like that it's ok. Thanks again!
When providing buttons for the dialog use
tr("Yes")
as for default dialogs, the created .ts-language file (to be edited via QtLinguist) should have default translations included.
The tr() marks the given argument to be translated. This concludes to if you do not know what will be written on a given label, you cannot translate it...

Resources