|
发表于 2009-5-17 13:52:51
|
显示全部楼层
dialog.cpp
- #include <QMessageBox>
- #include <QtGui>
- #include <QtCore/QTextCodec>
- #include <QtGui/QCloseEvent>
- #include <QMenu>
- #include "dialog.h"
- #include "ui_dialog.h"
- Dialog::Dialog(QWidget *parent)
- : QDialog(parent), ui(new Ui::Dialog)
- {
- char *string = "你好,世界!";
- // QTextCodec *codec = QTextCodec::codecForName("GBK");
- // QString strText = codec->toUnicode(string);
- // QString str;
- //str = str.fromLocal8Bit(string);
- this->setWindowFlags(Qt::WindowMinimizeButtonHint);
- ui->setupUi(this);
- ui->textEdit->clear();
- ui->textEdit->setText(QObject::tr(string));
- //QObject::connect(ui->pushButton2, SIGNAL(clicked()),this, SLOT(close()));
- //QObject::connect(ui->pushButton, SIGNAL(clicked()),this, SLOT(on_pushButton_clicked()));
- QIcon icon = QIcon("./hello.svg");
- setWindowIcon(icon);
- //this->restoreAction = new QAction(tr("还原 (&R)"), this);
- trayIcon = new QSystemTrayIcon(this);
- trayIcon->setIcon(icon);
- trayIcon->setToolTip("1st trip of QT4");
- QString titlec=QObject::tr("QT4");
- QString textc=QObject::tr("初次约会");
- trayIcon->show();
- createActions();
- createTrayIcon();
- trayIcon->show();
- trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,50);
- setWindowTitle(tr("QT4"));
- connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
- connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
- }
- Dialog::~Dialog()
- {
- delete ui;
- }
- void Dialog::on_pushButton2_clicked()
- {
- this->close();
- }
- void Dialog::on_pushButton_clicked()
- {
- QMessageBox::information(this,tr("朋友"),tr("您好"));
- }
- void Dialog::showM()
- {
- QString titlec=tr("双击打开主窗体");
- QString textc=QString::fromLocal8Bit("右键选择其他操作");
- trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
- }
- void Dialog::showMessage(char *msg)
- {
- QString titlec=tr(msg);
- QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
- trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
- }
- void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
- {
- switch (reason) {
- case QSystemTrayIcon::Trigger:
- //showMessage("单击");
- // this->showM();
- //connect(this->restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
- break;
- case QSystemTrayIcon::DoubleClick:
- this->showNormal();
- //showMessage("双击啦");
- break;
- //case QSystemTrayIcon::MiddleClick:
- //showMessage("你用的是三轮鼠标还是滚轮鼠标啊");
- // this->showM();
- break;
- default:
- ;
- }
- }
- void Dialog::createActions()
- {
- minimizeAction = new QAction(tr("最小化 (&I)"), this);
- connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
- //maximizeAction = new QAction(tr("最大化 (&X)"), this);
- //connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
- restoreAction = new QAction(tr("还原 (&R)"), this);
- connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
- quitAction = new QAction(tr("退出 (&Q)"), this);
- connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
- }
- void Dialog::createTrayIcon()
- {
- trayIconMenu = new QMenu(this);
- trayIconMenu->addAction(minimizeAction);
- //trayIconMenu->addAction(maximizeAction);
- trayIconMenu->addAction(restoreAction);
- trayIconMenu->addSeparator();
- trayIconMenu->addAction(quitAction);
- trayIcon->setContextMenu(trayIconMenu);
- }
- void Dialog::closeEvent(QCloseEvent *event)
- {
- if (this->trayIcon->isVisible())
- {
- QMessageBox::information(this, tr("QT4"),
- tr("程序会一直运行在系统托盘。您可以通过右键点击程序对应的系统托盘图标,然后选择菜单中"退出"选项来终止程序的运行。"));
- hide();
- event->ignore();
- }
- }
- void Dialog::changeEvent(QEvent * event )
- {
- if(event->WindowStateChange)
- {
- switch(this->windowState()){
- case Qt::WindowMinimized:
- //this->showNormal();
- this->hide();
- event->ignore();
- // event->accept();
- break;
- // case Qt::WindowNoState:
- //this->show();
- // event->ignore();
- // break;
- // default:
- break;
- }
- }
- }
- void Dialog::paintEvent(QPaintEvent *event)
- {
- QPainter painter;
- painter.begin(this);
- painter.setRenderHint(QPainter::Antialiasing);
- //helper->paint(&painter, event, elapsed);
- painter.end();
- }
复制代码 |
|