Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions
General Architecture Help
EStudley last edited by
I've done a lot of small QML and Qt applications, but not I'm starting on a large project for my senior year. I've been trying to implement my application the way I want it but I keep messing up and noticing I have to re-architecture my entire program. I just need some help before I go any further developing and have to re do it all again.
I want my application to be one main window that is entirely defined by QML. I want it to not close when the main window is closed, but only when the system icon is clicked and exited. I know to do this I need to set viewer.setQuitOnLastWindowClosed(false), but this also means I have to subclass ": public QSystemTrayIcon".
So should I create a Qt Widgets Application with base class MainWindow? How can I subclass QSystemTrayIcon if it's a MainWindow? Do I always need "qtquick2applicationviewer" to view QML?
I feel like every example I find for Qt is versions out of date and I just can't seem to put a large project together without it falling apart because I can't seem to get the pieces working together.
adolby last edited by
You can use QMainWindow with a system tray icon. Overriding the closeEvent method is the key to keeping the application from exiting except through the system tray icon. I added code in to restore the window when the user clicks the system tray icon. I also added code to inform the user the first time that he or she exits the application that the application will continue running in the system tray.
Here's a sketch I made up, inspired by the System Tray Example.
class MainWindow : public QMainWindow
explicit MainWindow(QWidget* parent = NULL);
void iconActivated(QSystemTrayIcon::ActivationReason reason);
virtual void closeEvent(QCloseEvent* event);
QSystemTrayIcon* trayIcon; QMenu* trayIconMenu; bool userInformed;
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIcon = new QSystemTrayIcon(this);
const QIcon icon(":/images/heart.png");
QWidget* mainWidget = new QWidget();
QVBoxLayout* mainLayout = new QVBoxLayout(mainWidget);
// Add your widgets here
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
void MainWindow::closeEvent(QCloseEvent* event)
QMessageBox::information(this, tr("Your App"),
tr("The application is still running in the system tray."));
userInformed = true;
int main(int argc, char *argv)
QApplication app(argc, argv);
QApplication::setQuitOnLastWindowClosed(false); MainWindow mainWindow; mainWindow.show(); return app.exec();