Results 1 to 12 of 12

Thread: QSystemTrayIcon doesnt seem to emit activated signal on icon click

  1. #1
    Join Date
    Oct 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSystemTrayIcon doesnt seem to emit activated signal on icon click

    i have this line in my header file to declare a qsystemtray icon

    private:
    QSystemTrayIcon *trayIcon ;

    The implementation file has these lines to instantiate a tray class and set up a signal/slot

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(QString("/usr/share/icons/zuluCrypt.png")));

    signal/slot connection is made with this line just below the above two lines in the implementation file:


    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon: :ActivationReason)),this,SLOT(trayClicked(QSystemT rayIcon::ActivationReason)));

    trayClicked function is prototyped as below in the header file:

    private slots :
    void trayClicked(QSystemTrayIcon::ActivationReason e) ;

    the command that shows the tray is "trayIcon->show();"

    Basically, i have
    1. created a pointer to class Qsystemtray in the private section of the header file.
    2. Instantiated a Qsystemtrayicon class and assign it to the pointer in the implementation file as shown above.
    3. connect the signal generated by the tray on click to a slot on my main window class
    4. The slot is prototyped in the "private slot" section of the header file.

    I think i have done everything i am supposed to but the slot doesnt get called(or the signal doesnt get generated) when i click on the tray icon and i have no idea why this is happened. What am i doing wrong?

  2. #2
    Join Date
    Oct 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    still struggling with this one.

    I am using kde 4.7 on linux.

    Anybody know of a simple Qt based application i can look at the code to see how they implemented the tray?

    I have seen Qt tutorials on implementing the tray icon and i believe i have done everything it says but it just doesnt work for me.

  3. #3
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    I made a program with QSystemTrayIcon some months ago.
    Here my code. It isn't full, I simplified it.

    header:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QSystemTrayIcon>
    6. #include <QNetworkReply>
    7. #include "mythread.h"
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. void createActions();
    24. void createTrayIcon();
    25.  
    26. QAction *quitAction;
    27. QAction *settingsAction;
    28.  
    29. QSystemTrayIcon *trayIcon;
    30. QMenu *trayIconMenu;
    31. public slots:
    32. void ment();
    33.  
    34. };
    To copy to clipboard, switch view to plain text mode 

    code:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QNetworkRequest>
    4. #include <QNetworkAccessManager>
    5. #include <QMenu>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. createActions();
    12. createTrayIcon();
    13. ui->setupUi(this);
    14. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ment()));
    15. trayIcon->show();
    16.  
    17. }
    18.  
    19. MainWindow::~MainWindow(){
    20. mt->exit();
    21. mt->wait();
    22. delete ui;
    23.  
    24. }
    25.  
    26. void MainWindow::createActions(){
    27. quitAction = new QAction(tr("&Kilepes"), this);
    28. connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    29. settingsAction = new QAction(tr("&Beallitasok"), this);
    30. connect(settingsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
    31. }
    32.  
    33. void MainWindow::createTrayIcon(){
    34. trayIconMenu = new QMenu(this);
    35. trayIconMenu->addAction(settingsAction);
    36. trayIconMenu->addAction(quitAction);
    37.  
    38. trayIcon = new QSystemTrayIcon(this);
    39. trayIcon->setContextMenu(trayIconMenu);
    40. QIcon icon(":/images/ok.svg");
    41. trayIcon->setIcon(icon);
    42. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    I think you forgot in your code to include the line that connect the signal generated by the tray when clicked to a slot.

    what part of the header file function "createTrayIcon()" is prototyped?

    Did your code for you? on what system?

    All i want is to click the tray to minimize the main window and to click it again to raise the window.

    The central widget of the main window is tablewidget, could it be eating up the click or something?

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    Do you get a warning on the command line? Like: "Object::connect: No such slot ..." Maybe it's a typo, but the reason for the question is the space in your connect statement:"trayClicked(QSystemT rayIcon::ActivationReason)"

    How are you determining that the slot isn't being called?

  6. #6
    Join Date
    Oct 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    that space was probably added by the forum when i pasted the code, its not on my code here. I am not getting any warnings on the command line, no error, no warnings.

    The implementations of the "trayClicked function is below

    void zuluCrypt::trayClicked(QSystemTrayIcon::Activation Reason e)
    {
    std::cout << "ds" << std::endl ;
    if( e == QSystemTrayIcon::Trigger){

    if(this->isVisible() == true)
    this->hide();
    else
    this->show();
    }
    }

    I am using Qt Creator and that "ds" string would have showed up in the "application output window" if i run the program from within Qt Creator if the function was called.

  7. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    Works fine here:
    Qt Code:
    1. #include <QtGui>
    2. #include <iostream>
    3.  
    4. class MainWindow : public QMainWindow {
    5.  
    6. Q_OBJECT
    7.  
    8. public:
    9. MainWindow(){
    10. QTableWidget *tw = new QTableWidget(3,3,this); // added re: your question above.
    11. setCentralWidget(tw);
    12. QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
    13. trayIcon->setIcon(QIcon("../icons/googleearth-icon.png"));
    14. trayIcon->show();
    15. connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(trayClicked(QSystemTrayIcon::ActivationReason)));
    16. }
    17.  
    18. private slots:
    19. void trayClicked(QSystemTrayIcon::ActivationReason e){
    20. std::cout << "ds" << std::endl;
    21. if( e == QSystemTrayIcon::Trigger){
    22. if(this->isVisible() == true) this->hide();
    23. else this->show();
    24. }
    25. }
    26. };
    27.  
    28.  
    29. int main(int argc, char *argv[]){
    30. QApplication app(argc, argv);
    31. MainWindow w;
    32. w.show();
    33. app.exec();
    34. }
    35.  
    36. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    Took a look at your app and your problem is that you have your mainwindow (zulucrypt) set to Qt::ApplicationModal

  9. #9
    Join Date
    Oct 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    I took your code, put in qt creator, build and run it and the tray shows on the system tray but without an icon which is understandable because i dont have that icon in that path but the tray does not respond to clicks but atleast now i have an output and it says:

    Object::connect: No such slot MainWindow::trayClicked(QSystemTrayIcon::Activatio nReason)

    so i guess this is progress.

    What tool did you use to write, build and run the code in? This could be a Qt creator 2.3.1 + Qt 4.7.4 specific issue

  10. #10
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    Quote Originally Posted by mhogomchungu View Post
    I am using Qt Creator and that "ds" string would have showed up in the "application output window" if i run the program from within Qt Creator if the function was called.
    Qt Code:
    1. #include <QDebug>
    2. qDebug() << "ds";
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click

    Did you read post #8? Try changing the zuluCrypt mainwindow from ApplicationModal to WindowModal and check the behavior.

    Quote Originally Posted by mhogomchungu View Post
    This could be a Qt creator 2.3.1 + Qt 4.7.4 specific issue
    Don't think so. I'm using QtCreator 2.2.1 & Qt 4.7.3 (64 bit).

    Try it from the command line:
    mkdir systraytest && cd systraytest
    touch main.cpp
    open main.cpp with your favorite editor and paste in the code. Save
    qmake -project
    qmake
    make
    ./systraytest

    Again, it works fine on a Debian Sid (KDE) box.

  12. #12
    Join Date
    Oct 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSystemTrayIcon doesnt seem to emit activated signal on icon click (solved)

    Thanks for the tip, i did not know this.

    Quote Originally Posted by entee View Post
    Qt Code:
    1. #include <QDebug>
    2. qDebug() << "ds";
    To copy to clipboard, switch view to plain text mode 
    thanks for the tip, i did not know this.


    Added after 7 minutes:


    Quote Originally Posted by norobro View Post
    Did you read post #8? Try changing the zuluCrypt mainwindow from ApplicationModal to WindowModal and check the behavior.
    No, i did not see post number 8 the first time.

    I changed the modal to windowModal and it now works. This should have been included in the qt docs examples. It was supper frustrating to follow their examples to the latter and then still getting ignored by QsystemTray.

    The problem i had didnt exist on the internet as far as mr. google is concerned so, i must be the only or first idiot to hit it :-)

    Thanks you very much, i have been struggling with this for days now.

    Did i already said thank you, if not, thank you very much :-)

    how do i edit the title to add "solved" to it? or you guys dont do that in this forum
    Last edited by mhogomchungu; 15th October 2011 at 22:47.

Similar Threads

  1. Replies: 7
    Last Post: 12th October 2016, 08:48
  2. programmatically right click on QSystemTrayIcon
    By noa l in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2011, 13:42
  3. signal doesnt emit
    By mark2804 in forum Newbie
    Replies: 2
    Last Post: 25th December 2008, 22:36
  4. emit the activated signal on a combobox
    By Equilibrium in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2007, 12:33
  5. QSystemTrayIcon on click() ?
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2007, 08:43

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.