سلام عليكم فى برنامج فى ال Examples تبع ال QT بإسم systray هتلقيه تحت ال QT/Examples/Desktop ودا تحليل المثال اول شئ ملف الresources ودا مضاف فيه بعض الصور اللى هتستخدم فى البرنامج شكل ال Application لاحظ الApplication متقسم إلى 2 GroupBoxes الأول هو TrayIcon وبيشمل Label بإسم Icon: و ComboBox فيه كذا Item زى Heart و Bad و Thrash و CheckBox -> Show icon التانى بيشمل Label مكتوب عليه type و ComboBox فيه الصور المستخدمة فى ال MessageBoxes ك informations و Label مكتوب عليه Duration و SpinBox وبعد كدا Label بإسم title وامامه LineEdit وتحته Label مكتوب عليه body وتحته RichEdit وفيه هيتكتب الرسالة اللى هتظهر ! واخيرا PushButton مكتوب عليه Show Message نبدأ الشغل .. افتح ال IDE اللى هتكتب فيها انا حاليا على Dev-C++ اولا اعمل ملف لل header وليكن window.h
#ifndef WINDOW_H #define WINDOW_H
#include <QSystemTrayIcon> #include <Qwidget>
class QAction; class QCheckBox; class QComboBox; class QGroupBox; class QLabel; class QLineEdit; class QMenu; class QPushButton; class QSpinBox; class QtextEdit;
class Window : public Qwidget
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(); الميثودز التالية هى اللى هنستخدمها فى عمل ال iconGroupBox وال MessageGroupBox وال Actions بتاعت ال menu وال trayicon 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;
}; #endif
#include "window.h"
#include <QtGui>
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); }
createIconGroupBox(); createMessageGroupBox();
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)));
void Window::setVisible(bool visible) { minimizeAction->setEnabled(visible); maximizeAction->setEnabled(!isMaximized()); restoreAction->setEnabled(isMaximized() || !visible); QWidget::setVisible(visible); }
void Window::setIcon(int index) { QIcon icon = iconComboBox->itemIcon(index); trayIcon->setIcon(icon); setWindowIcon(icon); trayIcon->setToolTip(iconComboBox->itemText(index)); }
void Window::showMessage() { QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon( typeComboBox->itemData(typeComboBox->currentIndex()).toInt()); trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, durationSpinBox->value() * 1000); }
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?")); }
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); }
#include "window.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); if (!QSystemTrayIcon::isSystemTrayAvailable()) { QMessageBox::critical(0, QObject::tr("Systray"), QObject::tr("I couldn't detect any system tray " "on this system.")); return 1; } Window window; window.show(); return app.exec(); }
~> qmake -project ~> qmake systray.pro ~> make