PDA

View Full Version : Help about a problem on the tray...



Bong.Da.City
18th August 2010, 00:36
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.

http://i37.tinypic.com/2rc1qg6.jpg

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

http://i37.tinypic.com/mh5952.jpg

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


QMessageBox msgBox;
msgBox.setText("<b>This is my <big>Clock</big> application<b>.");
msgBox.setInformativeText("If you like it, visit my <a href=http://www.qtforum.org>webpage</a>!");
msgBox.setWindowTitle("Clock v0.1 by bong.da.city!");
//set the icon of the message box to a custom pixmap of size 64x64
msgBox.setIconPixmap(QIcon(":/new/clock/clock.png").pixmap(QSize(64,64)));
msgBox.setWindowIcon(QIcon(":/new/clock/clock.png"));
msgBox.exec();

Urthas
18th August 2010, 01:14
I am only guessing here, but you may need to connect the QMessageBox::buttonClicked() signal to a custom slot.

ChrisW67
18th August 2010, 01:19
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.

Bong.Da.City
18th August 2010, 01:20
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?

Bong.Da.City
18th August 2010, 01:22
Take a look of the source code..


#include "roloi.h"
#include "ui_roloi.h"
#include "sett.h"
#include <QtCore/QTimer>
#include <QtCore/QDate>
#include "QMessageBox"
#include "QSystemTrayIcon"
#include "QCloseEvent"
#include "QMenu"
#include "QPalette"
#include "QColor"
#include "QColorDialog"

roloi::roloi(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::roloi)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTimeLabel()));
//this is the trayicon
trayIcon = new QSystemTrayIcon(this);
this->updateTimeLabel();
timer->start(1000);
this->setWindowFlags(Qt::WindowStaysOnTopHint);
//set the initial value of the label text on an exact hour
sharpColor.setNamedColor("red");
//this is where we set it's icon
trayIcon->setIcon(QIcon(":/new/clock/clock.png"));
//this is the tooltip
trayIcon->setToolTip("Clock - " + ui->timeLabel->text() + "\nDouble click to show the window.\nMiddle click to exit the application.");
//this is where we connect the activation of the icon (all the clicks on the icon in the tray) to our custom slot clickSysTrayIcon
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon: :ActivationReason)),this,SLOT(clickSysTrayIcon(QSy stemTrayIcon::ActivationReason)));

//this was very difficult to achieve, basicaly i had to do a custom signal in order for this to work
//that is connected to the window's hide() slot, but we have the queue the connection, as it can't actualy
//hide it until the system minimize animation is over - that's what Qt::QueuedConnection does - it waits
//for that to finish, and then it calls the hide() function of the main window
connect(this,SIGNAL(minimized()),this,SLOT(hide()) ,Qt::QueuedConnection);

//adding the menu on right click on the tray icon
QMenu *clock_menu = new QMenu;
clock_menu->addAction("&Show",this,SLOT(showNormal()));
clock_menu->addAction("Se&ttings",this,SLOT(showSettings()));
//this is how you add a menu action with an icon
clock_menu->addAction(QIcon(":/new/clock/clock.png"),"&About",this,SLOT(showAbout()));
clock_menu->addSeparator();
clock_menu->addAction("&Exit",qApp,SLOT(quit()));
trayIcon->setContextMenu(clock_menu);
}

roloi::~roloi()
{
delete ui;
}

void roloi::changeEvent(QEvent *e)
{
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
//see if the event is changing the window state
case QEvent::WindowStateChange:
//if it is, we need to see that this event is minimize
if (isMinimized()) {
//show the tray icon
trayIcon->show();
//emit the minimized signal and queueing the hide() slot we connected to earlier to happen after the
//system minimizes the window
emit minimized();
}
default:
break;
}
QMainWindow::changeEvent(e);
}

void roloi::updateTimeLabel()
{
//this basicaly takes the first 5 characters of the time string, starting from right to left
QString clockSharp = QTime::currentTime().toString().right(5);
//set a new pallete to color our label
QColor fg;
fg.setNamedColor("black");
QPalette pal = ui->timeLabel->palette();

//now we can check if this string is "00:00", which means the clock is x o'clock sharp
if (clockSharp == "18:40") {
fg = sharpColor;
}
pal.setColor(QPalette::Foreground,fg);
ui->timeLabel->setPalette(pal);
ui->timeLabel->setText(QTime::currentTime().toString());
trayIcon->setToolTip("Clock | " + ui->timeLabel->text() + "\n\nDouble click to show the window.\nMiddle click to exit the application.");
this->setWindowTitle("Clock | " + ui->timeLabel->text());
if (ui->timeLabel->text() == "12:21:00") {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Information);
msgBox.setText("Time for work!");
msgBox.setWindowTitle("Clock information!");
msgBox.exec();
}
}


void roloi::showSettings()
{
sett *gamatos = new sett(this);
gamatos->show();
}

void roloi::showAbout()
{
QMessageBox msgBox;
msgBox.setText("<b>This is my <big>Clock</big> application<b>.");
msgBox.setInformativeText("If you like it, visit my <a href=http://www.qtforum.org>webpage</a>!");
msgBox.setWindowTitle("Clock v0.1 by bong.da.city!");
//set the icon of the message box to a custom pixmap of size 64x64
msgBox.setIconPixmap(QIcon(":/new/clock/clock.png").pixmap(QSize(64,64)));
msgBox.setWindowIcon(QIcon(":/new/clock/clock.png"));
msgBox.exec();
}

Bong.Da.City
19th August 2010, 08:33
Shouldn't i probably minimize it somehow and then show the tray icon? Look what i had think...



void roloi::showAbout()
{
QMessageBox msgBox;
msgBox.setText("<b>This is my <big>Clock</big> application<b>.");
msgBox.setInformativeText("If you like it, visit my <a href=http://www.qtforum.org>webpage</a>!");
msgBox.setWindowTitle("Clock v0.1 by bong.da.city!");
//set the icon of the message box to a custom pixmap of size 64x64
msgBox.setIconPixmap(QIcon(":/new/clock/clock.png").pixmap(QSize(64,64)));
msgBox.setWindowIcon(QIcon(":/new/clock/clock.png"));
msgBox.exec();

Minimize Here!!!
trayIcon->show();
emit minimized();
}