Results 1 to 4 of 4

Thread: Minimize to Tray

  1. #1

    Default Minimize to Tray

    I have searched all around and cannot get this to work! Attempting to call hide() within changeEvent in a way such as:

    Qt Code:
    1. void AppWindow::changeEvent(QEvent *event)
    2. {
    3. if (event->type() == QEvent::WindowStateChange)
    4. if(isMinimized())
    5. {
    6. hide();
    7. }
    8. QMainWindow::changeEvent(event);
    9. }
    To copy to clipboard, switch view to plain text mode 

    causes the window to white-out rather than hide. Does anyone know why this happens or how to fix this? I am attempting to use a tray-icon on minimize, but without being able to hide will never have much success. Currently, the only way I can work around this is to use a singleShot timer to call the hide.

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Minimize to Tray

    Try this way (at least it works fine in our projects):
    Qt Code:
    1. if (event->type() == QEvent::WindowStateChange && windowState() & Qt::WindowMinimized)
    2. QMetaObject::invokeMethod(this, "hide", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

  3. #3

    Default Re: Minimize to Tray

    That's very neat, works great. Next problem:

    When I restore the application it loses it's window-size. It remembers it's location, but forgets the size. Is there a proper way to restore, or am I going to have to save the settings and restore them manually when brought back from the tray. I am using:

    showNormal();

    I have tried simply using 'show' and 'activateWindow' but this has the same flaw and in addition it fails to bring the window to the front.


    Added after 42 minutes:


    Currently I have fixed the problem using saveGeometry and restoreGeometry. However, if this is a needless work-around I'd like to drop it. Any ideas are welcome.
    Last edited by QT8seven; 5th December 2011 at 02:26.

  4. #4
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Minimize to Tray

    Maybe this will help:
    Qt Code:
    1. void MainWindow::restoreWindow()
    2. {
    3. setVisible(true);
    4. if(isMinimized())
    5. setWindowState(windowState() & ~Qt::WindowMinimized);
    6.  
    7. #ifdef Q_OS_WIN32
    8. ::SetForegroundWindow(effectiveWinId());
    9. ::SetWindowPos(effectiveWinId(), HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    10. #else
    11. activateWindow();
    12. #endif
    13. }
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

Similar Threads

  1. Help about a problem on the tray...
    By Bong.Da.City in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2010, 07:33
  2. Minimize to system tray
    By aLiNuSh in forum Newbie
    Replies: 17
    Last Post: 4th March 2010, 12:51
  3. Minimize to system tray
    By krivenok in forum Qt Programming
    Replies: 5
    Last Post: 13th January 2009, 04:34
  4. Tray Ballon
    By eu.x in forum Newbie
    Replies: 5
    Last Post: 28th February 2007, 12:00
  5. QT4/QP Tray Icon
    By incapacitant in forum Newbie
    Replies: 12
    Last Post: 7th June 2006, 18:47

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.