Qt QHBoxLayout percentage size

void QSizePolicy::setHorizontalStretch(uchar stretchFactor) Example: QHBoxLayout* layout = new QHBoxLayout(form); QWidget* left = new QWidget(form); QSizePolicy spLeft(QSizePolicy::Preferred, QSizePolicy::Preferred); spLeft.setHorizontalStretch(1); left->setSizePolicy(spLeft); layout->addWidget(left); QWidget* right = new QWidget(form); QSizePolicy spRight(QSizePolicy::Preferred, QSizePolicy::Preferred); spRight.setHorizontalStretch(2); right->setSizePolicy(spRight); layout->addWidget(right);

What is the correct way of QSqlDatabase & QSqlQuery?

When you create a QSqlDatabase object with addDatabase or when you call removeDatabase, you are merely associating or disassociating a tuple (driver, hostname:port, database name, username/password) to a name (or to the default connection name if you don’t specify a connection name). The SQL driver is instantiated, but the database will only be opened when … Read more

easiest way to parse JSON in Qt 4.7

JSON parsing is now supported in Qt 5. Here’s how to load and parse a document: #include <QByteArray> #include <QFile> #include <QJsonObject> #include <QJsonDocument> // … // Read JSON file QFile file(“/path/to/file.json”); file.open(QIODevice::ReadOnly); QByteArray rawData = file.readAll(); // Parse document QJsonDocument doc(QJsonDocument::fromJson(rawData)); // Get JSON object QJsonObject json = doc.object(); // Access properties qDebug() << … Read more

How to insert BLOB and CLOB files in MySQL?

Two ways: 1 – Use a LOAD_FILE function – INSERT INTO table1 VALUES(1, LOAD_FILE(‘data.png’)); 2 – Insert file as hex string, e.g. – INSERT INTO table1 VALUES (1, x’89504E470D0A1A0A0000000D494844520000001000000010080200000090916836000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000001E49444154384F6350DAE843126220493550F1A80662426C349406472801006AC91F1040F796BD0000000049454E44AE426082′);

How to copy Qt runtime DLLs to project output

OK, here’s an ugly hack: # Copy required DLLs to output directory CONFIG(debug, debug|release) { QtCored4.commands = copy /Y %QTDIR%\\bin\\QtCored4.dll debug QtCored4.target = debug/QtCored4.dll QtGuid4.commands = copy /Y %QTDIR%\\bin\\QtGuid4.dll debug QtGuid4.target = debug/QtGuid4.dll QMAKE_EXTRA_TARGETS += QtCored4 QtGuid4 PRE_TARGETDEPS += debug/QtCored4.dll debug/QtGuid4.dll } else:CONFIG(release, debug|release) { QtCore4.commands = copy /Y %QTDIR%\\bin\\QtCore4.dll release QtCore4.target = release/QtCore4.dll QtGui4.commands … Read more

Is it possible to connect a signal to a static slot without a receiver instance?

Update for QT5: Yes you can static void someFunction() { qDebug() << “pressed”; } // … somewhere else QObject::connect(button, &QPushButton::clicked, someFunction); In QT4 you can’t: No it is not allowed. Rather, it is allowed to use a slot which is a static function, but to be able to connect it you need an instance. In … Read more