Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to check if the application has been minimized



  • int main(int argc, char *argv[])
    {
        QApplication application(argc, argv);
        Renderer w(model ); // This is  QWidget
        w.show();
        QObject::connect(&w, &QWindow::windowStateChanged, [&](Qt::WindowState state) {
            });
    
            // how will i define the QObject::connect 
        return application.exec();
    }
    
    What would be the parameters for the QObject::connect function ?
    

  • Lifetime Qt Champion

    @summit said in How to check if the application has been minimized:

    What would be the parameters for the QObject::connect function ?

    You already have the connect, so what is the question?
    Just check state == Qt::WindowMinimized in your lambda...



  • @jsulm No this does not connect it gives an error during compile time , no instance of overloaded function matches the argument list.

    so i try this

    QWindow *wndQT = (QWindow*)w.window();
    		QObject::connect(wndQT, &QWindow::windowStateChanged , [&](Qt::WindowState state) {
    			qDebug() << "Minimized";
    		});
    
    This is able to compile but the lambda is never executed.
    

  • Lifetime Qt Champion

    @summit w.window() returns a QWidget, not QWindow.
    Try

    QObject::connect(QApplication::activeWindow(), &QWindow::windowStateChanged , [&](Qt::WindowState state) {
    			qDebug() << "Minimized";
    		});
    


  • @jsulm This does not compile as QApplication::activeWindow() also returns QWidget*

    i have to cast it to compile.

    QObject::connect((QWindow*)QApplication::activeWindow(), &QWindow::windowStateChanged , [&](Qt::WindowState state) {
    qDebug() << "Minimized";
    });

    But still lambda does not execute.



  • Using QApplication::focusWindow() i was able to get the window.

    QObject::connect(QApplication::focusWindow(), &QWindow::windowStateChanged , [&](Qt::WindowState state) {
    qDebug() << "Minimized";
    });



  • @summit
    Just for the record, you do actually need/ought check the value of the state parameter to the lambda:

    if (state == Qt::WindowMinimized) qDebug() << "Minimized";
    

    ?



  • @JonB Yes i am doing that.


Log in to reply