Results 1 to 6 of 6

Thread: Help about a problem on the tray...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Help about a problem on the tray...

    I have an application that when you minimize it it goes to tray.. If you right click on the icon on the tray it show a menu.. See screenshot.



    So when i press the About a QMessageBox is been shown.. See screenshot.



    The problem is that when i press ok or close the QmessageBox the application closes.. I want to go back to the tray.. How can i do this?

    This is what happens when you press About

    Qt Code:
    1. QMessageBox msgBox;
    2. msgBox.setText("<b>This is my <big>Clock</big> application<b>.");
    3. msgBox.setInformativeText("If you like it, visit my <a href=http://www.qtforum.org>webpage</a>!");
    4. msgBox.setWindowTitle("Clock v0.1 by bong.da.city!");
    5. //set the icon of the message box to a custom pixmap of size 64x64
    6. msgBox.setIconPixmap(QIcon(":/new/clock/clock.png").pixmap(QSize(64,64)));
    7. msgBox.setWindowIcon(QIcon(":/new/clock/clock.png"));
    8. msgBox.exec();
    To copy to clipboard, switch view to plain text mode 
    Last edited by Bong.Da.City; 17th August 2010 at 23:41.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Help about a problem on the tray...

    I am only guessing here, but you may need to connect the QMessageBox::buttonClicked() signal to a custom slot.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help about a problem on the tray...

    Post some code telling us what the program does after you click the OK button on the dialog and the exec() call returns. The snippet above is not useful.

  4. #4
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help about a problem on the tray...

    Xmm .. Yes i also believe that.. But first i do not how to go to the slot of this button because it is a QMessageBox ( A friend hat created this application )
    Do you want to give you a link of the project?

  5. #5
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help about a problem on the tray...

    Take a look of the source code..

    Qt Code:
    1. #include "roloi.h"
    2. #include "ui_roloi.h"
    3. #include "sett.h"
    4. #include <QtCore/QTimer>
    5. #include <QtCore/QDate>
    6. #include "QMessageBox"
    7. #include "QSystemTrayIcon"
    8. #include "QCloseEvent"
    9. #include "QMenu"
    10. #include "QPalette"
    11. #include "QColor"
    12. #include "QColorDialog"
    13.  
    14. roloi::roloi(QWidget *parent) :
    15. QMainWindow(parent),
    16. ui(new Ui::roloi)
    17. {
    18. ui->setupUi(this);
    19. QTimer *timer = new QTimer(this);
    20. connect(timer, SIGNAL(timeout()), this, SLOT(updateTimeLabel()));
    21. //this is the trayicon
    22. trayIcon = new QSystemTrayIcon(this);
    23. this->updateTimeLabel();
    24. timer->start(1000);
    25. this->setWindowFlags(Qt::WindowStaysOnTopHint);
    26. //set the initial value of the label text on an exact hour
    27. sharpColor.setNamedColor("red");
    28. //this is where we set it's icon
    29. trayIcon->setIcon(QIcon(":/new/clock/clock.png"));
    30. //this is the tooltip
    31. trayIcon->setToolTip("Clock - " + ui->timeLabel->text() + "\nDouble click to show the window.\nMiddle click to exit the application.");
    32. //this is where we connect the activation of the icon (all the clicks on the icon in the tray) to our custom slot clickSysTrayIcon
    33. connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(clickSysTrayIcon(QSystemTrayIcon::ActivationReason)));
    34.  
    35. //this was very difficult to achieve, basicaly i had to do a custom signal in order for this to work
    36. //that is connected to the window's hide() slot, but we have the queue the connection, as it can't actualy
    37. //hide it until the system minimize animation is over - that's what Qt::QueuedConnection does - it waits
    38. //for that to finish, and then it calls the hide() function of the main window
    39. connect(this,SIGNAL(minimized()),this,SLOT(hide()),Qt::QueuedConnection);
    40.  
    41. //adding the menu on right click on the tray icon
    42. QMenu *clock_menu = new QMenu;
    43. clock_menu->addAction("&Show",this,SLOT(showNormal()));
    44. clock_menu->addAction("Se&ttings",this,SLOT(showSettings()));
    45. //this is how you add a menu action with an icon
    46. clock_menu->addAction(QIcon(":/new/clock/clock.png"),"&About",this,SLOT(showAbout()));
    47. clock_menu->addSeparator();
    48. clock_menu->addAction("&Exit",qApp,SLOT(quit()));
    49. trayIcon->setContextMenu(clock_menu);
    50. }
    51.  
    52. roloi::~roloi()
    53. {
    54. delete ui;
    55. }
    56.  
    57. void roloi::changeEvent(QEvent *e)
    58. {
    59. switch (e->type()) {
    60. case QEvent::LanguageChange:
    61. ui->retranslateUi(this);
    62. break;
    63. //see if the event is changing the window state
    64. case QEvent::WindowStateChange:
    65. //if it is, we need to see that this event is minimize
    66. if (isMinimized()) {
    67. //show the tray icon
    68. trayIcon->show();
    69. //emit the minimized signal and queueing the hide() slot we connected to earlier to happen after the
    70. //system minimizes the window
    71. emit minimized();
    72. }
    73. default:
    74. break;
    75. }
    76. QMainWindow::changeEvent(e);
    77. }
    78.  
    79. void roloi::updateTimeLabel()
    80. {
    81. //this basicaly takes the first 5 characters of the time string, starting from right to left
    82. QString clockSharp = QTime::currentTime().toString().right(5);
    83. //set a new pallete to color our label
    84. QColor fg;
    85. fg.setNamedColor("black");
    86. QPalette pal = ui->timeLabel->palette();
    87.  
    88. //now we can check if this string is "00:00", which means the clock is x o'clock sharp
    89. if (clockSharp == "18:40") {
    90. fg = sharpColor;
    91. }
    92. pal.setColor(QPalette::Foreground,fg);
    93. ui->timeLabel->setPalette(pal);
    94. ui->timeLabel->setText(QTime::currentTime().toString());
    95. trayIcon->setToolTip("Clock | " + ui->timeLabel->text() + "\n\nDouble click to show the window.\nMiddle click to exit the application.");
    96. this->setWindowTitle("Clock | " + ui->timeLabel->text());
    97. if (ui->timeLabel->text() == "12:21:00") {
    98. QMessageBox msgBox;
    99. msgBox.setIcon(QMessageBox::Information);
    100. msgBox.setText("Time for work!");
    101. msgBox.setWindowTitle("Clock information!");
    102. msgBox.exec();
    103. }
    104. }
    105.  
    106.  
    107. void roloi::showSettings()
    108. {
    109. sett *gamatos = new sett(this);
    110. gamatos->show();
    111. }
    112.  
    113. void roloi::showAbout()
    114. {
    115. QMessageBox msgBox;
    116. msgBox.setText("<b>This is my <big>Clock</big> application<b>.");
    117. msgBox.setInformativeText("If you like it, visit my <a href=http://www.qtforum.org>webpage</a>!");
    118. msgBox.setWindowTitle("Clock v0.1 by bong.da.city!");
    119. //set the icon of the message box to a custom pixmap of size 64x64
    120. msgBox.setIconPixmap(QIcon(":/new/clock/clock.png").pixmap(QSize(64,64)));
    121. msgBox.setWindowIcon(QIcon(":/new/clock/clock.png"));
    122. msgBox.exec();
    123. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help about a problem on the tray...

    Shouldn't i probably minimize it somehow and then show the tray icon? Look what i had think...

    Qt Code:
    1. void roloi::showAbout()
    2. {
    3. QMessageBox msgBox;
    4. msgBox.setText("<b>This is my <big>Clock</big> application<b>.");
    5. msgBox.setInformativeText("If you like it, visit my <a href=http://www.qtforum.org>webpage</a>!");
    6. msgBox.setWindowTitle("Clock v0.1 by bong.da.city!");
    7. //set the icon of the message box to a custom pixmap of size 64x64
    8. msgBox.setIconPixmap(QIcon(":/new/clock/clock.png").pixmap(QSize(64,64)));
    9. msgBox.setWindowIcon(QIcon(":/new/clock/clock.png"));
    10. msgBox.exec();
    11.  
    12. Minimize Here!!!
    13. trayIcon->show();
    14. emit minimized();
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to keep System tray icons
    By mkkguru in forum Qt Programming
    Replies: 10
    Last Post: 24th February 2010, 16:19
  2. system tray problem
    By vvdounai in forum Qt Programming
    Replies: 5
    Last Post: 14th November 2007, 08:25
  3. Tray Icon
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2007, 19:10
  4. Tray Ballon
    By eu.x in forum Newbie
    Replies: 5
    Last Post: 28th February 2007, 12:00
  5. QT4/QP Tray Icon
    By incapacitant in forum Newbie
    Replies: 12
    Last Post: 7th June 2006, 18:47

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
  •  
Qt is a trademark of The Qt Company.