Results 1 to 6 of 6

Thread: Can not activate trayicon when dialog is open

  1. #1
    Join Date
    Feb 2019
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Can not activate trayicon when dialog is open

    I am working on a program that can minimize to tray and hide the main window. I expect the mainwindow to show when I click on the tray icon. This usually works, but I find that if the program minimizes to tray when I call QFileDialog::getOpenFileName() to select file without closing the dialog, I cannot activate the trayicon. Does anyone know how to solve it? I am using Qt 5.12.1 on Manjaro Linux. Here is my example:

    main.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h:

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. QSystemTrayIcon *trayIcon;
    5. QMenu *trayContextMenu;
    6. QAction *actShow;
    7. QPushButton *button;
    8.  
    9. public:
    10. MainWindow(QWidget *parent = 0);
    11.  
    12. protected:
    13. void changeEvent(QEvent *);
    14.  
    15. private slots:
    16. void click();
    17. void trayIcon_activated(QSystemTrayIcon::ActivationReason reason);
    18. void actShow_Triggered();
    19. };
    20.  
    21. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QFileDialog>
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent)
    6. {
    7. QPushButton *button = new QPushButton("button", this);
    8. setCentralWidget(button);
    9. connect(button,&QPushButton::clicked,
    10. this,&MainWindow::click);
    11. trayIcon=new QSystemTrayIcon;
    12. trayIcon->setIcon(QIcon("hmtimer.png"));
    13. trayContextMenu=new QMenu;
    14. actShow=trayContextMenu->addAction(tr("Show"));
    15. trayIcon->setContextMenu(trayContextMenu);
    16. connect(actShow,&QAction::triggered,
    17. this,&MainWindow::actShow_Triggered);
    18. connect(trayIcon,&QSystemTrayIcon::activated,
    19. this,&MainWindow::trayIcon_activated);
    20. }
    21.  
    22. void MainWindow::changeEvent(QEvent *event)
    23. {
    24. if(event->type()==QEvent::WindowStateChange){
    25. if(isMinimized()){
    26. this->hide();
    27. trayIcon->show();
    28. }
    29. }
    30. else{
    31. QMainWindow::changeEvent(event);
    32. }
    33. }
    34.  
    35. void MainWindow::click()
    36. {
    37. QFileDialog::getOpenFileName(this,QString());
    38. }
    39.  
    40. void MainWindow::trayIcon_activated(QSystemTrayIcon::ActivationReason reason)
    41. {
    42. if(reason==3){ //reason==Trigger
    43. this->show();
    44. trayIcon->hide();
    45. }
    46. }
    47.  
    48. void MainWindow::actShow_Triggered()
    49. {
    50. this->show();
    51. trayIcon->hide();
    52. }
    To copy to clipboard, switch view to plain text mode 

    The example can be downloaded here

    It seems that if the program minimizes to tray when QFileDialog::getOpenFileName(this,QString()) is not closed, I cannot make the mainwindow appear by click on the tray icon or right click to show the context menu.

    How to reproduce:

    1. Open the program
    2. Click on the button to open file dialog
    3. Minimize to tray
    4. Try clicking and right-clicking on the tray icon

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can not activate trayicon when dialog is open

    Well, the file dialog is "modal" on the parent window, keeping use events away from the parent as long as it is opened.

    Maybe the tray icon is considered part of that part of the UI or the event sent by the tray integration to the main window is considered part of the types to filter.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    cges30901 (14th February 2019)

  4. #3
    Join Date
    Feb 2019
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Can not activate trayicon when dialog is open

    Thank you for your reply. So maybe I should not minimize to tray when the file dialog is shown?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can not activate trayicon when dialog is open

    Right.

    I think it makes sense to show the main window when the dialog is opened, otherwise the user sees a dialog but doesn't have a reference which program this belongs to.

    Cheers,
    _

  6. #5
    Join Date
    Feb 2019
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Can not activate trayicon when dialog is open

    OK, this is the code that doesn't minimize to tray when dialog is open:

    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QSystemTrayIcon>
    6. #include <QPushButton>
    7. #include <QEvent>
    8. #include <QMenu>
    9. #include <QAction>
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14. QSystemTrayIcon *trayIcon;
    15. QMenu *trayContextMenu;
    16. QAction *actShow;
    17. QPushButton *button;
    18. bool dialog_open;
    19.  
    20. public:
    21. MainWindow(QWidget *parent = 0);
    22.  
    23. protected:
    24. void changeEvent(QEvent *);
    25.  
    26. private slots:
    27. void click();
    28. void trayIcon_activated(QSystemTrayIcon::ActivationReason reason);
    29. void actShow_Triggered();
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QFileDialog>
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent)
    6. {
    7. QPushButton *button = new QPushButton("button", this);
    8. setCentralWidget(button);
    9. connect(button,&QPushButton::clicked,
    10. this,&MainWindow::click);
    11. trayIcon=new QSystemTrayIcon;
    12. trayIcon->setIcon(QIcon("hmtimer.png"));
    13. trayContextMenu=new QMenu;
    14. actShow=trayContextMenu->addAction(tr("Show"));
    15. trayIcon->setContextMenu(trayContextMenu);
    16. connect(actShow,&QAction::triggered,
    17. this,&MainWindow::actShow_Triggered);
    18. connect(trayIcon,&QSystemTrayIcon::activated,
    19. this,&MainWindow::trayIcon_activated);
    20. dialog_open=false;
    21. }
    22.  
    23. void MainWindow::changeEvent(QEvent *event)
    24. {
    25. if(event->type()==QEvent::WindowStateChange
    26. && dialog_open==false){
    27. if(isMinimized()){
    28. this->hide();
    29. trayIcon->show();
    30. }
    31. }
    32. else{
    33. QMainWindow::changeEvent(event);
    34. }
    35. }
    36.  
    37. void MainWindow::click()
    38. {
    39. dialog_open=true;
    40. QFileDialog::getOpenFileName(this,QString());
    41. dialog_open=false;
    42. }
    43.  
    44. void MainWindow::trayIcon_activated(QSystemTrayIcon::ActivationReason reason)
    45. {
    46. if(reason==3){ //reason==Trigger
    47. this->show();
    48. trayIcon->hide();
    49. }
    50. }
    51.  
    52. void MainWindow::actShow_Triggered()
    53. {
    54. this->show();
    55. trayIcon->hide();
    56. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Feb 2019
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Can not activate trayicon when dialog is open

    Just found that this problem is related to the operating system/desktop environment. I am using xfce, and when I minimize to tray, both mainwindow and dialog are hided.
    On IceWM, only mainwindow is hided, and dialog is still shown.
    On Gnome, seems that it doesn't have system tray.
    On windows, both dialog and mainwindow are not hided, and tray icon is not shown (nothing happens at all).

Similar Threads

  1. Replies: 3
    Last Post: 8th August 2016, 14:20
  2. Open File Dialog Help
    By "BumbleBee" in forum Newbie
    Replies: 6
    Last Post: 20th February 2011, 11:46
  3. Replies: 0
    Last Post: 10th September 2010, 14:23
  4. Open Dialog from MainWindow.
    By halvors in forum Qt Programming
    Replies: 8
    Last Post: 1st April 2010, 02:09
  5. Open/saveFile dialog bug?
    By macbeth in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2007, 16:20

Tags for this Thread

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.