Results 1 to 13 of 13

Thread: Toggle dialog visibility on application switch

  1. #1
    Join Date
    Jun 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Toggle dialog visibility on application switch

    Hi,

    I am working on an application containing several tool dialogs and that can have several document windows open simultaneously.
    In order for the tool dialogs to be accessible at all times, I've set the AlwaysOnTop flag.
    However, I don't want my tool dialogs to stay on top of dialogs of other applications when the user switches to another application (basically I would want to be able to reproduce the behavior of e.g. the palettes in Photoshop).
    Does anyone know how to achieve this?
    Thanks in advance.

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Toggle dialog visibility on application switch

    try something like this:

    Qt Code:
    1. // main.cpp
    2. MyApplication a(argc, argv);
    3. MainWindow w;
    4. QObject::connect(&a, SIGNAL(activated()), w.getDialog(), SLOT(show()));
    5. QObject::connect(&a, SIGNAL(deactivated()), w.getDialog(), SLOT(hide()));
    6. w.show();
    7.  
    8.  
    9. // application class header
    10. class MyApplication : public QApplication
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MyApplication(int argc, char **argv);
    16. ~MyApplication();
    17.  
    18. signals:
    19. void activated();
    20. void deactivated();
    21.  
    22. private:
    23. bool eventFilter(QObject *obj, QEvent *event);
    24. };
    25.  
    26. // application class implementation
    27. MyApplication::MyApplication(int argc, char **argv): QApplication(argc, argv)
    28. {
    29. installEventFilter(this);
    30. }
    31.  
    32. bool MyApplication::eventFilter(QObject *obj, QEvent *event)
    33. {
    34. if (event->type() == QEvent::ApplicationActivate)
    35. emit activated();
    36. else if (event->type() == QEvent::ApplicationDeactivate)
    37. emit deactivated();
    38.  
    39. return false;
    40. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Toggle dialog visibility on application switch

    isn't this exactly what Qt:Tool window flag is for?
    By default, tool windows will disappear when the application is inactive.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Jun 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Toggle dialog visibility on application switch

    Quote Originally Posted by mentalmushroom View Post
    try something like this:

    Qt Code:
    1. // main.cpp
    2. MyApplication a(argc, argv);
    3. MainWindow w;
    4. QObject::connect(&a, SIGNAL(activated()), w.getDialog(), SLOT(show()));
    5. QObject::connect(&a, SIGNAL(deactivated()), w.getDialog(), SLOT(hide()));
    6. w.show();
    7.  
    8.  
    9. // application class header
    10. class MyApplication : public QApplication
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MyApplication(int argc, char **argv);
    16. ~MyApplication();
    17.  
    18. signals:
    19. void activated();
    20. void deactivated();
    21.  
    22. private:
    23. bool eventFilter(QObject *obj, QEvent *event);
    24. };
    25.  
    26. // application class implementation
    27. MyApplication::MyApplication(int argc, char **argv): QApplication(argc, argv)
    28. {
    29. installEventFilter(this);
    30. }
    31.  
    32. bool MyApplication::eventFilter(QObject *obj, QEvent *event)
    33. {
    34. if (event->type() == QEvent::ApplicationActivate)
    35. emit activated();
    36. else if (event->type() == QEvent::ApplicationDeactivate)
    37. emit deactivated();
    38.  
    39. return false;
    40. }
    To copy to clipboard, switch view to plain text mode 
    I've tried that, but unfortunately the ApplicationDeactivate is sent when closing a window while remaining in the same application.


    Added after 4 minutes:


    Quote Originally Posted by amleto View Post
    isn't this exactly what Qt:Tool window flag is for?
    It probably is, but doesn't work that way on Windows: when I specify the Qt::Tool flag without the Qt::AlwaysOnTop flag my tool palettes don't stay on top of my document windows.
    Last edited by gvanvoor; 18th June 2012 at 16:55.

  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Toggle dialog visibility on application switch

    the ApplicationDeactivate is sent when closing a window while remaining in the same application
    I've tried it with a one modal dialog and I had no such problem. Maybe you can try to set some flag when a window is closed, so you can distinguish this kind of event then (but don't forget to reset it).

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Toggle dialog visibility on application switch

    Qt::Tool should work just fine on Windows.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Toggle dialog visibility on application switch

    Quote Originally Posted by wysota View Post
    Qt::Tool should work just fine on Windows.
    It doesn't in my particular case :-(
    My document windows are QMainWindows with the default flags, my tools palette have Qt::Tool | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowCloseButtonHint flags (the WindowStaysOnTopHint is only there to prevent the tools windows to hide under the document windows, but given the unwanted side effects I'ld like ot get rid of it).
    I'm using Qt 4.8 on Windows 7.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Toggle dialog visibility on application switch

    Quote Originally Posted by gvanvoor View Post
    It doesn't in my particular case :-(
    Maybe you are using it wrong. Try the window flags example (or demo) that comes with Qt and see if that works.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Toggle dialog visibility on application switch

    Quote Originally Posted by wysota View Post
    Maybe you are using it wrong. Try the window flags example (or demo) that comes with Qt and see if that works.
    I might be using it wrongly, but in that case someone will undoubtedly find what's wrong in this minimal code sample that reproduces the problem:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. QMainWindow * theDocumentWindow = new QMainWindow();
    6. theDocumentWindow->setWindowTitle( "Document Window" );
    7. theDocumentWindow->setCentralWidget( new QLabel( "Document" ) );
    8. theDocumentWindow->show();
    9.  
    10. QWidget * theToolsPalette = new QWidget( NULL, Qt::Tool | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint );
    11. theToolsPalette->setWindowTitle( "Tools Palette" );
    12. QHBoxLayout * thePaletteLayout = new QHBoxLayout( theToolsPalette );
    13. thePaletteLayout->addWidget( new QLabel( "Tools" ) );
    14. theToolsPalette->show();
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    In the real program window creation is done in the application of course.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Toggle dialog visibility on application switch

    Why don't you just try the example I pointed you to?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jun 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Toggle dialog visibility on application switch

    Quote Originally Posted by wysota View Post
    Why don't you just try the example I pointed you to?
    I did, but couldn't find a combination of flags that resulted in the desired behavior. Am I doing something wrong or just expecting too much? The only solution I see at this point is to write a class that raises the tool palettes every time a (document) window has been activated.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Toggle dialog visibility on application switch

    So this doesn't work?

    Qt Code:
    1. cat main.cpp
    2. #include <QtGui>
    3.  
    4. int main(int argc, char **argv) {
    5. QApplication app(argc, argv);
    6. QWidget d(&w);
    7. d.setWindowFlags(Qt::Tool);
    8. w.show();
    9. d.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jun 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Toggle dialog visibility on application switch

    Quote Originally Posted by wysota View Post
    So this doesn't work?

    Qt Code:
    1. cat main.cpp
    2. #include <QtGui>
    3.  
    4. int main(int argc, char **argv) {
    5. QApplication app(argc, argv);
    6. QWidget d(&w);
    7. d.setWindowFlags(Qt::Tool);
    8. w.show();
    9. d.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    This probably works, but as I have more than one Document Window and the tools need to stay on top of all of them I can't use that :-(

Similar Threads

  1. Replies: 1
    Last Post: 13th June 2013, 09:43
  2. How to get toggle action text?
    By sudhansu in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2012, 11:57
  3. Toggle text for context menu.
    By phillip_Qt in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2010, 12:29
  4. How to Switch the dialog Form
    By Smiler in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2009, 11:36
  5. How to toggle between 2 forms
    By safknw in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2006, 10:34

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.