Results 1 to 18 of 18

Thread: Minimize to system tray

  1. #1
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Minimize to system tray

    Hi,

    I have a subclassed QDialog and I implemented a QSystemTrayIcon for it, with some Show/Hide options.
    What I want to do is to minimize the window to the tray when the minimize button is clicked. I managed to do it for the close button by reimplementing QDialog::closeEvent () but I want to do it for the minimize button.

    So any solutions?

    Thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    You can use QWindowStateChangeEvent, and look at the current window state. This event must be handled in the event() function of your dialog.

    Regards.

  3. #3
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Quote Originally Posted by marcel View Post
    You can use QWindowStateChangeEvent, and look at the current window state. This event must be handled in the event() function of your dialog.

    Regards.
    So I could do something like...

    Qt Code:
    1. void MyDialog::event (QEvent * event)
    2. {
    3. // i have no idea what i should write here, maybe like...
    4. QWindowStateChangeEvent *qwsc_event = event;
    5. Qt::WindowStates flag = qwsc_event->oldState ();
    6. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Qt Code:
    1. void MyDialog::event( QEvent *e )
    2. {
    3. if( e->type() == QEvent::WindowStateChange )
    4. {
    5. //Now check to see if the window is minimised
    6. if( isMinimised() )
    7. //Do something here (show the icon in the systray )
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Should work

    Edit:
    don't do a dynamic cast at every event, instead check the event type

  5. #5
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Quote Originally Posted by marcel View Post
    Qt Code:
    1. void MyDialog::event( QEvent *e )
    2. {
    3. if( e->type() == QEvent::WindowStateChange )
    4. {
    5. //Now check to see if the window is minimised
    6. if( isMinimised() )
    7. //Do something here (show the icon in the systray )
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Should work

    Edit:
    don't do a dynamic cast at every event, instead check the event type
    OK I will try it, but shouldn't I do something like forward the other events? How would I do that... like when an event is not a WindowStateChange how would I forward it for default processing? Should I do something like...

    Qt Code:
    1. void MyDialog::event( QEvent *e )
    2. {
    3. if( e->type() == QEvent::WindowStateChange )
    4. {
    5. //Now check to see if the window is minimised
    6. if( isMinimised() )
    7. {
    8. hide ();
    9. e->ignore ();
    10. }
    11. }
    12. else e->accept ();
    13. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Sure .
    It was just an on-the-fly example.
    Of course you have to forward the other events.

    Regards

  7. #7
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Quote Originally Posted by marcel View Post
    Sure .
    It was just an on-the-fly example.
    Of course you have to forward the other events.

    Regards
    Well actually the function returns a bool so I did this:

    Qt Code:
    1. bool MyInstantMessenger::event (QEvent *e)
    2. {
    3. if(e->type() == QEvent::WindowStateChange)
    4. {
    5.  
    6. //Now check to see if the window is minimised
    7. if (isMinimized())
    8. {
    9. e->accept ();
    10. hide ();
    11. }
    12. else e->ignore ();
    13. return true;
    14. }
    15. e->ignore ();
    16. return false;
    17. }
    To copy to clipboard, switch view to plain text mode 

    But this messes my application, like the widgets are not drawn and the QCloseEvent is not sent anymore. I also tried setting other combinations like ignoring the message instead of accepting it and returning false instead of true but that didn't either.

    Would installEventFilter() work too? I'm looking in to it right now...

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Minimize to system tray

    One should pass the events to the base class implementation. Also, it's recommended to reimplement more specialized QWidget::changeEvent() instead of QWidget::event().
    Qt Code:
    1. void MyInstantMessenger::changeEvent (QEvent *e)
    2. {
    3. if(e->type() == QEvent::WindowStateChange)
    4. {
    5. //Now check to see if the window is minimised
    6. if (isMinimized())
    7. {
    8. hide ();
    9. }
    10. }
    11. QDialog::changeEvent(e); // QDialog, or whatever the base class is
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Yes, passing the ignored events to the base class will solve your problem.

  10. #10
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    That didn't help either
    I also tried accepting and/or ignoring events and also didn't work, the window is still not hidden from the taskbar and the client area not painted.

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Qt Code:
    1. bool MyInstantMessenger::event (QEvent *e)
    2. {
    3. if(e->type() == QEvent::WindowStateChange)
    4. {
    5. //Now check to see if the window is minimised
    6. if (isMinimized())
    7. {
    8. hide ();
    9. }
    10. }
    11. return QDialog::event( e );
    12. }
    To copy to clipboard, switch view to plain text mode 
    ]

    Is this what you did?

    EDIT: This is what you need to do. You have to let QDialog handle the change event. You must not accept nor ignore the event. Just hide your window.

    regards
    Last edited by marcel; 17th April 2007 at 08:21.

  12. The following user says thank you to marcel for this useful post:

    aLiNuSh (17th April 2007)

  13. #12
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Quote Originally Posted by marcel View Post
    Qt Code:
    1. bool MyInstantMessenger::event (QEvent *e)
    2. {
    3. if(e->type() == QEvent::WindowStateChange)
    4. {
    5. //Now check to see if the window is minimised
    6. if (isMinimized())
    7. {
    8. e->accept ();
    9. hide ();
    10. return true;
    11. }
    12. }
    13. return QDialog::event( e );
    14. }
    To copy to clipboard, switch view to plain text mode 
    ]

    Is this what you did?
    Maybe... I tried so many different combinations I can't remember anymore.
    Anyways I tried it now and it does the same thing... when I push the minimize button the window minimizes to the taskbar and the taskbar "button for the window" disappears and appears again quickly, and then when I maximize it, the window shows on the screen but the client area is not painted. Obviously it has to be something with some of the events not being forwarded for default processing... i just don't know what I'm missing here...

  14. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    Could you show me the actual code? The one that you last tried

  15. #14
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Minimize to system tray

    I just closed Notepad++ and deleted the code... basically I've tried reimplementing changeEvent () or event () sometimes accepting or rejecting the events, sometimes returning true or false or QWidget::changeEvent ()/event() and other different combinations.
    I lost 4 hours on this stupid thing and still haven't got it working.

    Anyways thanks for the assistance and maybe when I'll feel masochist enough I'll retry it :P

    We're from the same country BTW

    LE: tried the edited one too.. same thing
    Last edited by aLiNuSh; 17th April 2007 at 09:13.

  16. #15
    Join Date
    Jan 2009
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Minimize to system tray

    Quote Originally Posted by aLiNuSh View Post
    That didn't help either
    I also tried accepting and/or ignoring events and also didn't work, the window is still not hidden from the taskbar and the client area not painted.
    I faced the same problem.

    Anyone has a solution to these issues?

    Thanks in advance.

  17. #16
    Join Date
    Jun 2009
    Location
    Austria
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Minimize to system tray

    Hi !

    I also have the problem with the systrayicon and minimize
    so i try following ...

    Qt Code:
    1. // Overload original event-handler:
    2. bool MainWindow::event(QEvent *evt)
    3. {
    4. // Check if Window-State changed to minimize ...
    5. if(evt->type() == QEvent::WindowStateChange)
    6. {
    7. //Now check to see if the window is minimised
    8. if (isMinimized())
    9. {
    10. // Call the Hide Slot after 250ms
    11. // to prozess other events ....
    12. qApp->processEvents();
    13. QTimer::singleShot(250, this, SLOT(hide()));
    14. evt->ignore();
    15. }
    16. }
    17. // Call original-handler (in this case QMainWindow ...)
    18. return QMainWindow::event(evt);
    19. }
    To copy to clipboard, switch view to plain text mode 

    ... this work's on windows !

  18. #17
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Smile Re: Minimize to system tray

    Use the below code it might be helpful to you.......

    Qt Code:
    1. //System tray icon action context menu
    2.  
    3. minimizeAction = new QAction(tr("Mi&nimize"), this);
    4. connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
    5.  
    6. maximizeAction = new QAction(tr("Ma&ximize"), this);
    7. connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
    8.  
    9. restoreAction = new QAction(tr("&Restore"), this);
    10. connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
    11.  
    12. quitAction = new QAction(tr("&Quit"), this);
    13. connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    14.  
    15. //ToolbarAction(itemExit).icon(), ToolbarAction(itemExit).text(), parent);
    16. cmenu.addAction(tr("Show"), this, SLOT(show()));
    17. cmenu.addAction(tr("Hide"), this, SLOT(hide()));
    18. cmenu.addSeparator();
    19. cmenu.addAction(minimizeAction);
    20. cmenu.addAction(maximizeAction);
    21. cmenu.addAction(restoreAction);
    22. cmenu.addAction(quitAction);
    To copy to clipboard, switch view to plain text mode 

  19. #18
    Join Date
    Mar 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Red face A solution - Re: Minimize to system tray

    Hi, here is a solution, and I think it works quite well in my application:

    In the dialog's slot that connects with the activated() signal of the QSystemTrayIcon object, use the setParent() function as:
    Qt Code:
    1. void FtpDialog::sysTrayActivated(QSystemTrayIcon::ActivationReason reason)
    2. {
    3. if (reason == QSystemTrayIcon::Trigger){
    4. setParent(NULL, Qt::Window);
    5. showNormal();
    6. sysTrayIcon->hide();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    And in the dialog's event() function:

    Qt Code:
    1. bool FtpDialog::event(QEvent *e)
    2. {
    3. if (e->type() == QEvent::WindowStateChange) {
    4. if (isMinimized()) {
    5. setParent(tmp, Qt::SubWindow);
    6. sysTrayIcon->show();
    7. e->ignore();
    8. qDebug()<<"event() called";
    9. } else {
    10. e->accept();
    11. }
    12.  
    13. }
    14. return QDialog::event(e); // return with QDialog's event() call anyway.
    15. }
    To copy to clipboard, switch view to plain text mode 

    Here, 'tmp' is an empty QWidget serving as a provisional 'parent' of the dialog, so that the dialog can be hidden from task-bar when minimized. In fact, you can just pass NULL as the first argument for the 'parent' widget, but in this case probably the re-stored window would exhibit slightly different with the original window size.

Similar Threads

  1. Hide on Minimize
    By defunct in forum Qt Programming
    Replies: 9
    Last Post: 26th March 2009, 09:46
  2. Minimize to system tray
    By krivenok in forum Qt Programming
    Replies: 5
    Last Post: 13th January 2009, 04:34
  3. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  4. System title bar height
    By andynugent in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th October 2006, 15:57
  5. How to change system menu of window?
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2006, 15:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.