Inserting an image in a GUI using Qt Designer

You can start inserting a label. Next, you right click on it an then click “change rich text…”. A new window will pop-up. Click on the figure Icon “Insert figure”. Now, you have to create a resource file. Click on the Pencil button. Next, click on the “New resource file” button. Choose a name and … Read more

qt.qpa.plugin: Could not load the Qt platform plugin “xcb” in “” even though it was found.

For the Qt 6.5.0 release, the missing library was libxcb-cursor.so.0, resolved with sudo apt install libxcb-cursor0 With that, the sample widget and gui projects from QtCreator run correctly built with Qt 6.5.0. libxcb-cursor.so.0 wasn’t previously needed for Qt 6.4.0. I identified the missing library with this shell code for l in /path/to/Qt/6.5.0/gcc_64/lib/*.so; do echo $l; … Read more

How do I handle the event of the user pressing the ‘X’ (close) button?

If you have a QMainWindow you can override closeEvent method. #include <QCloseEvent> void MainWindow::closeEvent (QCloseEvent *event) { QMessageBox::StandardButton resBtn = QMessageBox::question( this, APP_NAME, tr(“Are you sure?\n”), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, QMessageBox::Yes); if (resBtn != QMessageBox::Yes) { event->ignore(); } else { event->accept(); } } If you’re subclassing a QDialog, the closeEvent will not be called … Read more

How do I set the QComboBox width to fit the largest item?

Qt (4.6) online documentation has this to say about QComboBox: enum SizeAdjustPolicy { AdjustToContents, AdjustToContentsOnFirstShow, AdjustToMinimumContentsLength, AdjustToMinimumContentsLengthWithIcon } I would suggest ensuring the SizeAdjustPolicy is actually being used setting the enum to AdjustToContents. As you mention a .ui file I suggest doing that in Designer. Normally there shouldn’t be anything fancy in your constructor at … Read more

How could Qt apply style from an external Qt Stylesheet file?

Say the user have its stylesheet named stylesheet.qss and is located in the application folder. You could load the style sheet when starting the application, using the -stylesheet argument : myapp->stylesheet = stylesheet.qss; But this require your user to know how to start an application with arguments. What you could also do is to add … Read more

Reading entire file to QString

As pointed out, there is no such constructor for QTextStream. I quickly typed those few lines to verify it is indeed working properly: foreach (QString file, files) { QFile f(file); if (!f.open(QFile::ReadOnly | QFile::Text)) break; QTextStream in(&f); qDebug() << f.size() << in.readAll(); } And I do get the expected output – the size and content … Read more