Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSystemTrayIcon hide() crash on mac while I use QTimer to change the icon of QSystemTrayIcon
Qt 6.11 is out! See what's new in the release blog

QSystemTrayIcon hide() crash on mac while I use QTimer to change the icon of QSystemTrayIcon

Scheduled Pinned Locked Moved General and Desktop
21 Posts 5 Posters 25.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #3

    this is also tested in qtdemo's systray example , see the code I modified with timer
    window.h

    **
    ****************************************************************************/
    @
    #ifndef WINDOW_H
    #define WINDOW_H

    #include <QSystemTrayIcon>
    #include <QDialog>

    QT_BEGIN_NAMESPACE
    class QAction;
    class QCheckBox;
    class QComboBox;
    class QGroupBox;
    class QLabel;
    class QLineEdit;
    class QMenu;
    class QPushButton;
    class QSpinBox;
    class QTextEdit;
    QT_END_NAMESPACE

    //! [0]
    class Window : public QDialog
    {
    Q_OBJECT

    public:
    Window();

    void setVisible(bool visible);
    

    protected:
    void closeEvent(QCloseEvent *event);

    private slots:
    void setIcon(int index);
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
    void showMessage();
    void messageClicked();

    void startFlashing();
    void stopFlashing();
    protected:
    virtual void timerEvent(QTimerEvent *ev);

    private:
    int fTimerID;
    int fIndex;
    private:
    void createIconGroupBox();
    void createMessageGroupBox();
    void createActions();
    void createTrayIcon();

    QGroupBox *iconGroupBox;
    QLabel *iconLabel;
    QComboBox *iconComboBox;
    QCheckBox *showIconCheckBox;
    
    QGroupBox *messageGroupBox;
    QLabel *typeLabel;
    QLabel *durationLabel;
    QLabel *durationWarningLabel;
    QLabel *titleLabel;
    QLabel *bodyLabel;
    QComboBox *typeComboBox;
    QSpinBox *durationSpinBox;
    QLineEdit *titleEdit;
    QTextEdit *bodyEdit;
    QPushButton *showMessageButton;
    
    QAction *minimizeAction;
    QAction *maximizeAction;
    QAction *restoreAction;
    QAction *quitAction;
    
    QSystemTrayIcon *trayIcon;
    QMenu *trayIconMenu;
    

    };
    //! [0]

    #endif
    @

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #4

      window.cpp

      **
      ****************************************************************************/
      @
      #include <QtGui>

      #include "window.h"

      //! [0]
      Window::Window()
      {
      createIconGroupBox();
      createMessageGroupBox();

      iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
      
      createActions();
      createTrayIcon();
      
      connect(showMessageButton, SIGNAL(clicked()), this, SLOT(showMessage()));
      connect(showIconCheckBox, SIGNAL(toggled(bool)),
              trayIcon, SLOT(setVisible(bool)));
      connect(iconComboBox, SIGNAL(currentIndexChanged(int)),
              this, SLOT(setIcon(int)));
      connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
      connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
              this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
      
      QVBoxLayout *mainLayout = new QVBoxLayout;
      mainLayout->addWidget(iconGroupBox);
      mainLayout->addWidget(messageGroupBox);
      setLayout(mainLayout);
      
      iconComboBox->setCurrentIndex(1);
      trayIcon->show();
      
      setWindowTitle(tr("Systray"));
      resize(400, 300);
      

      }
      //! [0]

      //! [1]
      void Window::setVisible(bool visible)
      {
      minimizeAction->setEnabled(visible);
      maximizeAction->setEnabled(!isMaximized());
      restoreAction->setEnabled(isMaximized() || !visible);
      QDialog::setVisible(visible);
      }
      //! [1]

      //! [2]
      void Window::closeEvent(QCloseEvent *event)
      {
      if (trayIcon->isVisible()) {
      QMessageBox::information(this, tr("Systray"),
      tr("The program will keep running in the "
      "system tray. To terminate the program, "
      "choose <b>Quit</b> in the context menu "
      "of the system tray entry."));
      hide();
      event->ignore();
      }
      }
      //! [2]

      void Window::startFlashing()
      {
      trayIcon->show();
      fTimerID = startTimer(500);

      }
      void Window::stopFlashing()
      {
      killTimer(fTimerID);
      trayIcon->hide();
      }
      void Window::timerEvent(QTimerEvent *ev)
      {
      fIndex++;
      if(fIndex%2 ==0){
      trayIcon->setIcon(iconComboBox->itemIcon(0));
      }else
      trayIcon->setIcon(iconComboBox->itemIcon(1));
      }

      //! [3]
      void Window::setIcon(int index)
      {
      QIcon icon = iconComboBox->itemIcon(index);
      trayIcon->setIcon(icon);
      setWindowIcon(icon);

      trayIcon->setToolTip(iconComboBox->itemText(index));
      

      }
      //! [3]

      //! [4]
      void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
      {
      switch (reason) {
      case QSystemTrayIcon::Trigger:
      stopFlashing();
      break;
      case QSystemTrayIcon::DoubleClick:
      iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1)
      % iconComboBox->count());
      break;
      case QSystemTrayIcon::MiddleClick:
      showMessage();
      break;
      default:
      ;
      }
      }
      //! [4]

      //! [5]
      void Window::showMessage()
      {
      QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(
      typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
      trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
      durationSpinBox->value() * 1000);
      //trayIcon->hide();
      startFlashing();
      }
      //! [5]

      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #5

        //! [6]
        @
        void Window::messageClicked()
        {
        QMessageBox::information(0, tr("Systray"),
        tr("Sorry, I already gave what help I could.n"
        "Maybe you should try asking a human?"));
        }
        //! [6]

        void Window::createIconGroupBox()
        {
        iconGroupBox = new QGroupBox(tr("Tray Icon"));

        iconLabel = new QLabel("Icon:");
        
        iconComboBox = new QComboBox;
        iconComboBox->addItem(QIcon(":/images/bad.svg"), tr("Bad"));
        iconComboBox->addItem(QIcon(":/images/heart.svg"), tr("Heart"));
        iconComboBox->addItem(QIcon(":/images/trash.svg"), tr("Trash"));
        
        showIconCheckBox = new QCheckBox(tr("Show icon"));
        showIconCheckBox->setChecked(true);
        
        QHBoxLayout *iconLayout = new QHBoxLayout;
        iconLayout->addWidget(iconLabel);
        iconLayout->addWidget(iconComboBox);
        iconLayout->addStretch();
        iconLayout->addWidget(showIconCheckBox);
        iconGroupBox->setLayout(iconLayout);
        

        }

        void Window::createMessageGroupBox()
        {
        messageGroupBox = new QGroupBox(tr("Balloon Message"));

        typeLabel = new QLabel(tr("Type:"));
        
        typeComboBox = new QComboBox;
        typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
        typeComboBox->addItem(style()->standardIcon(
                QStyle::SP_MessageBoxInformation), tr("Information"),
                QSystemTrayIcon::Information);
        typeComboBox->addItem(style()->standardIcon(
                QStyle::SP_MessageBoxWarning), tr("Warning"),
                QSystemTrayIcon::Warning);
        typeComboBox->addItem(style()->standardIcon(
                QStyle::SP_MessageBoxCritical), tr("Critical"),
                QSystemTrayIcon::Critical);
        typeComboBox->setCurrentIndex(1);
        
        durationLabel = new QLabel(tr("Duration:"));
        
        durationSpinBox = new QSpinBox;
        durationSpinBox->setRange(5, 60);
        durationSpinBox->setSuffix(" s");
        durationSpinBox->setValue(15);
        
        durationWarningLabel = new QLabel(tr("(some systems might ignore this "
                                             "hint)"));
        durationWarningLabel->setIndent(10);
        
        titleLabel = new QLabel(tr("Title:"));
        
        titleEdit = new QLineEdit(tr("Cannot connect to network"));
        
        bodyLabel = new QLabel(tr("Body:"));
        
        bodyEdit = new QTextEdit;
        bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
                                  "clue.nClick this balloon for details."));
        
        showMessageButton = new QPushButton(tr("Show Message"));
        showMessageButton->setDefault(true);
        
        QGridLayout *messageLayout = new QGridLayout;
        messageLayout->addWidget(typeLabel, 0, 0);
        messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
        messageLayout->addWidget(durationLabel, 1, 0);
        messageLayout->addWidget(durationSpinBox, 1, 1);
        messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
        messageLayout->addWidget(titleLabel, 2, 0);
        messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
        messageLayout->addWidget(bodyLabel, 3, 0);
        messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
        messageLayout->addWidget(showMessageButton, 5, 4);
        messageLayout->setColumnStretch(3, 1);
        messageLayout->setRowStretch(4, 1);
        messageGroupBox->setLayout(messageLayout);
        

        }

        void Window::createActions()
        {
        minimizeAction = new QAction(tr("Mi&nimize"), this);
        connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

        maximizeAction = new QAction(tr("Ma&ximize"), this);
        connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
        
        restoreAction = new QAction(tr("&Restore"), this);
        connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
        
        quitAction = new QAction(tr("&Quit"), this);
        connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
        

        }

        void Window::createTrayIcon()
        {
        trayIconMenu = new QMenu(this);
        trayIconMenu->addAction(minimizeAction);
        trayIconMenu->addAction(maximizeAction);
        trayIconMenu->addAction(restoreAction);
        trayIconMenu->addSeparator();
        trayIconMenu->addAction(quitAction);

        trayIcon = new QSystemTrayIcon(this);
        trayIcon->setContextMenu(trayIconMenu);
        

        }
        @
        is this a qt bug ? or something I did wrong somewhere

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #6

          Oh god, please format your code with the '@' tag.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #7

            sorry , I copy it from mac , as I was not familiar with the format of mac textEdit , it is strange Its format is so messy , I will try to format it

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #8

              sorry could you tell me detailedly how to format it ?

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #9

                strange , when I edit it , the format looks well , but why when I post , it is so ugly .

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #10

                  [quote author="brucewuu" date="1309868142"]@//! [6]

                  void Window::messageClicked()
                  {
                  QMessageBox::information(0, tr("Systray"),
                  tr("Sorry, I already gave what help I could.n"
                  "Maybe you should try asking a human?"));
                  }

                  //! [6]

                  void Window::createIconGroupBox()
                  {
                  iconGroupBox = new QGroupBox(tr("Tray Icon"));

                  iconLabel = new QLabel("Icon:");
                  
                  iconComboBox = new QComboBox;
                  iconComboBox->addItem(QIcon(":/images/bad.svg"), tr("Bad"));
                  iconComboBox->addItem(QIcon(":/images/heart.svg"), tr("Heart"));
                  iconComboBox->addItem(QIcon(":/images/trash.svg"), tr("Trash"));
                  
                  showIconCheckBox = new QCheckBox(tr("Show icon"));
                  showIconCheckBox->setChecked(true);
                  
                  QHBoxLayout *iconLayout = new QHBoxLayout;
                  iconLayout->addWidget(iconLabel);
                  iconLayout->addWidget(iconComboBox);
                  iconLayout->addStretch();
                  iconLayout->addWidget(showIconCheckBox);
                  iconGroupBox->setLayout(iconLayout);
                  

                  }

                  void Window::createMessageGroupBox()
                  {
                  messageGroupBox = new QGroupBox(tr("Balloon Message"));

                  typeLabel = new QLabel(tr("Type:"));
                  
                  typeComboBox = new QComboBox;
                  typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
                  typeComboBox->addItem(style()->standardIcon(
                          QStyle::SP_MessageBoxInformation), tr("Information"),
                          QSystemTrayIcon::Information);
                  typeComboBox->addItem(style()->standardIcon(
                          QStyle::SP_MessageBoxWarning), tr("Warning"),
                          QSystemTrayIcon::Warning);
                  typeComboBox->addItem(style()->standardIcon(
                          QStyle::SP_MessageBoxCritical), tr("Critical"),
                          QSystemTrayIcon::Critical);
                  typeComboBox->setCurrentIndex(1);
                  
                  durationLabel = new QLabel(tr("Duration:"));
                  
                  durationSpinBox = new QSpinBox;
                  durationSpinBox->setRange(5, 60);
                  durationSpinBox->setSuffix(" s");
                  durationSpinBox->setValue(15);
                  
                  durationWarningLabel = new QLabel(tr("(some systems might ignore this "
                                                       "hint)"));
                  durationWarningLabel->setIndent(10);
                  
                  titleLabel = new QLabel(tr("Title:"));
                  
                  titleEdit = new QLineEdit(tr("Cannot connect to network"));
                  
                  bodyLabel = new QLabel(tr("Body:"));
                  
                  bodyEdit = new QTextEdit;
                  bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
                                            "clue.nClick this balloon for details."));
                  
                  showMessageButton = new QPushButton(tr("Show Message"));
                  showMessageButton->setDefault(true);
                  
                  QGridLayout *messageLayout = new QGridLayout;
                  messageLayout->addWidget(typeLabel, 0, 0);
                  messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
                  messageLayout->addWidget(durationLabel, 1, 0);
                  messageLayout->addWidget(durationSpinBox, 1, 1);
                  messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
                  messageLayout->addWidget(titleLabel, 2, 0);
                  messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
                  messageLayout->addWidget(bodyLabel, 3, 0);
                  messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
                  messageLayout->addWidget(showMessageButton, 5, 4);
                  messageLayout->setColumnStretch(3, 1);
                  messageLayout->setRowStretch(4, 1);
                  messageGroupBox->setLayout(messageLayout);
                  

                  }

                  void Window::createActions()
                  {
                  minimizeAction = new QAction(tr("Mi&nimize"), this);
                  connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

                  maximizeAction = new QAction(tr("Ma&ximize"), this);
                  connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
                  
                  restoreAction = new QAction(tr("&Restore"), this);
                  connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
                  
                  quitAction = new QAction(tr("&Quit"), this);
                  connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
                  

                  }

                  void Window::createTrayIcon()
                  {
                  trayIconMenu = new QMenu(this);
                  trayIconMenu->addAction(minimizeAction);
                  trayIconMenu->addAction(maximizeAction);
                  trayIconMenu->addAction(restoreAction);
                  trayIconMenu->addSeparator();
                  trayIconMenu->addAction(quitAction);

                  trayIcon = new QSystemTrayIcon(this);
                  trayIcon->setContextMenu(trayIconMenu);
                  

                  }
                  @
                  is this a qt bug ? or something I did wrong somewhere[/quote]

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #11

                    If you start and end your code section in your post with an 'at' sign ('@') it will look much nicer. That is what the others are asking for.

                    I have just added in the post above these two signs in the begin at the end. And voila it looks much better.

                    Sorry, had to do two posts ;-)

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • EddyE Offline
                      EddyE Offline
                      Eddy
                      wrote on last edited by
                      #12

                      there is a wiki page explaining how the editor works :
                      "Using the editor :":http://developer.qt.nokia.com/wiki/ForumHelp#9bd9c32b79efb1b2d5b039e4d48300a9

                      Qt Certified Specialist
                      www.edalsolutions.be

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #13

                        ok , guys , thanks very much , I have changed them now ~~anyone could give me solution ?thanks

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #14

                          It may be caused by some internal objects that are no longer available after you hide the icon. I can reproduce the crash on my Mac box.

                          As a workaround you can replace the call to hide with a single shot timer in your slot like this

                          @
                          fSystemTrayTimer->stop() ;
                          //fSystemTrayIcon->hide();
                          QTimer::singleShot(0, fSystemTrayIcon, SLOT(hide()));
                          @

                          This way the call to hide is delivered after you have left the slot.

                          EDIT:
                          I've added this as a doc not to [[Doc:QSystemTrayIcon]] too.

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #15

                            thanks Volker , you are always the best problem solver for me . Great Appreciation ~~~

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #16

                              hi Volker , I also meet the same problem as the following link
                              http://svn.netlabs.org/qt4/ticket/201
                              do you have some solution for that ?thanks ~~

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #17

                                I'll have a look into this later.

                                1 Reply Last reply
                                0
                                • ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #18

                                  thanks very much , I have read the guide to ask questions , it is very helpful for me , thank you very much , I will do this better ~~

                                  1 Reply Last reply
                                  0
                                  • ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by
                                    #19

                                    I didn't forget you but I was terribly busy the last week and am off for vacation soon, so please be a bit more patient.

                                    1 Reply Last reply
                                    0
                                    • ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #20

                                      thanks Volker ~~just have a look when you have time , thanks

                                      1 Reply Last reply
                                      0
                                      • ? Offline
                                        ? Offline
                                        A Former User
                                        wrote on last edited by
                                        #21

                                        [quote author="brucewuu" date="1309959197"]hi Volker , I also meet the same problem as the following link
                                        http://svn.netlabs.org/qt4/ticket/201
                                        do you have some solution for that ?thanks ~~[/quote]

                                        That's out of scope for me. I don't know the internal workings for the OS/2 port, sorry.

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved