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

Minimize to Tray in Gnome 2



  • Hi,

    I'm working with Qt 4.6 in Debian 6 with a Gnome 2 desktop environment.
    I'm trying to do a "Minimize to Tray" functionality but I can't make it work properly.

    Here is how I hide the window:
    @
    bool MyWindow::event(QEvent *event){

    if( event->type() == QEvent::WindowStateChange ){

    if( isMinimized() ){

    hide();
    return true;
    }
    }

    return QMainWindow::event(event);
    }
    @

    Then I call QWidget::showNormal() when the user clicks on the system tray icon.

    This seems to work fine except when I switch workspace in Gnome (e.g. Ctrl + Alt + Right). For instance if the window is displayed on workspace 1, when I switch to workspace 2 and back to 1 the window is minimized to tray. The reason is that when I switch workspace the window receive the same events when I click on the minimize button: A QHideEvent with spontaneous set to true then a QWindowStateChangeEvent.

    Does anyone have an idea how I could detect workspace switch in Gnome and keep the window displayed while the user switches back and forth between workspaces ?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean using something like QSystemTrayIcon ?



  • I'm already using QSystemTrayIcon, here is a more complete extract from my code:

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QSystemTrayIcon *icon = new QSystemTrayIcon(this);
    icon->show();
    
    QObject::connect(icon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systemTrayIconActivated(QSystemTrayIcon::ActivationReason)));
    

    }

    bool MainWindow::event(QEvent *event)
    {
    if(event->type() == QEvent::WindowStateChange)
    {
    if( isMinimized() )
    hide();
    }
    }

    void MainWindow::systemTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
    {
    if( reason == QSystemTrayIcon::Trigger )
    {
    showNormal();
    }
    }@

    The problem is that doing it this way the window is completely hidden when the user minimizes it (what I want) but also when the system minimizes it, i.e. workspace switch (not what I want).


  • Lifetime Qt Champion

    Then you might want to check QEvent::spontaneous() const



  • As I mentioned in my first post I already checked the value returned by spontaneous() , it is TRUE is both cases (minimized from the window title bar and minimized when workspace switches)


  • Lifetime Qt Champion

    Sorry, I've missed that. And it sounds strange the both returns true


Log in to reply