Results 1 to 5 of 5

Thread: Catch maximize and restore events.

  1. #1

    Default Catch maximize and restore events.

    I need to know when a window is restored or maximized. When this state-change happens, I need to take an action. I need to be careful however that this action only occurs once per state-change. I have the following code:

    Qt Code:
    1. void TextArea::changeEvent(QEvent* event)
    2. {
    3. if (event->type() == QEvent::WindowStateChange)
    4. {
    5. if (static_cast<QWindowStateChangeEvent*>(event)->oldState() == windowState())
    6. {
    7. return;
    8. }
    9. if (isMaximized())
    10. {
    11.  
    12. }
    13. else
    14. {
    15.  
    16. }
    17. }
    18. QWidget::changeEvent(event);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Even with the preceding code, the action is firing twice with the old-state being 0 and new state being 2. Is there any way to ensure that this fires only once?

  2. #2

    Default Re: Catch maximize and restore events.

    Am I going to need to correct this by filtering out with timer-tick-counts?

    Edit:
    Currently I am thinking about doing a single-shot timer which calls a function that checks the window-state and takes action based on the window-state at that time. With this I could ensure that the action occurs only once as well as that the action taken matches the current window-state. This would cause a delay in the action, but seems to be the best option currently. Please let me know if I am doing something wrong which could be fixed to avoid this method.

    Edit
    While I have not found a better way to fix this, I want to add some information for any future people that find this thread:

    It seems that the changeEvent is called twice for maximizing a window and once for restoring it down to it's original size. Perhaps someone can add more information as to why and perhaps how to segregate these triggers. For now, I am using a single-shot timer with '0' passed for time. Since this puts it at the end of the message-queue it causes no delay and will not be called twice.
    Last edited by QT8seven; 27th December 2011 at 05:37.

  3. #3
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Catch maximize and restore events.

    This works for me:

    Qt Code:
    1. void MyQtApp::changeEvent(QEvent *e)
    2. {
    3. if(e->type() == QEvent::WindowStateChange) {
    4.  
    5. if(isMinimized()) {
    6.  
    7. // MINIMIZED
    8.  
    9. } else {
    10.  
    11. // NORMAL/MAXIMIZED ETC
    12.  
    13. }
    14. }
    15.  
    16. e->accept();
    17. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Catch maximize and restore events.

    Qt Code:
    1. void MainWindow::changeEvent( QEvent* e )
    2. {
    3. if( e->type() == QEvent::WindowStateChange )
    4. {
    5. QWindowStateChangeEvent* event = static_cast< QWindowStateChangeEvent* >( e );
    6.  
    7. if( event->oldState() & Qt::WindowMinimized )
    8. {
    9. qDebug() << "Window restored (to normal or maximized state)!";
    10. }
    11. else if( event->oldState() == Qt::WindowNoState && this->windowState() == Qt::WindowMaximized )
    12. {
    13. qDebug() << "Window Maximized!";
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    Change event is emitted for many reasons so you can see it more than once during single WindowStateChange.
    You should not worry about that anyway because changes you're interested in are happening only once.
    From what you said I understand that you need to know when window is restored from minimized state (to normal or maximized) and when it's maximized from normal state.
    You're not interested in knowing when window was minimized, right?
    Code above does just that without need for timer.

  5. #5
    Join Date
    Mar 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Catch maximize and restore events.

    Worked well, Spitfire. Thanks so much!
    Last edited by bkudrle; 25th January 2014 at 01:56. Reason: updated contents

Similar Threads

  1. MDI - Child -- Restore Down
    By QT8seven in forum Newbie
    Replies: 4
    Last Post: 19th December 2011, 13:38
  2. maximize grid with maximize window
    By bibhukalyana in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2011, 08:16
  3. Can I use delegate to catch mouse and keyboard events
    By hubbobubbo in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2010, 12:00
  4. How to restore the position of the textCursor?
    By Dark_Tower in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2006, 19:41
  5. My dialog can't restore after minimizing?
    By gtthang in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2006, 15:18

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.