Mainscreen Active Window State Trouble



  • Hi I am having a small problem with my main window class derived from QMainWindow. I am trying to test if the window is active with keyboard focus using the following code. I want to show notification area messages when it's not active. The only problem is the code fires even when the mainwindow is active. Any ideas?

    if (!(windowState().testFlag(Qt::WindowActive)))
            if (notifications)
            {
                QIcon icon(":/images/icons/rep.ico");
                trayIcon->showMessage("Rep", message, icon, 500);
            }
    


  • @Crag_Hack said in Mainscreen Active Window State Trouble:

    Qt::WindowActive

    You cant check if QWindow::isActive() returns true? Another Solution i found would be

    bool MainWindow::event(QEvent * e) // overloading event(QEvent*) method of QMainWindow 
    {
        switch(e->type())
        {
            // ...
    
            case QEvent::WindowActivate :
                // gained focus
                break ;
    
            case QEvent::WindowDeactivate :
                // lost focus
                break ;
            // ...
        } ;
        return QMainWindow::event(e) ;
    }
    


  • Thanks Fuel. IsActiveWindow() didn't work so I just used ishidden() and isminimized() which isn't perfect but good enough for now. I'll try the event overload when I have time.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.