#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"
ui(new Ui::roloi)
{
ui->setupUi(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTimeLabel()));
//this is the trayicon
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
//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
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()) {
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;
}
}
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
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;
}
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") {
msgBox.setText("Time for work!");
msgBox.setWindowTitle("Clock information!");
msgBox.exec();
}
}
void roloi::showSettings()
{
sett *gamatos = new sett(this);
gamatos->show();
}
void roloi::showAbout()
{
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();
}
#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(QSystemTrayIcon::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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks