QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8519|回复: 16

基础篇 -- Qt4.2之后的托盘类实现托盘程序

[复制链接]
发表于 2008-3-9 20:28:42 | 显示全部楼层 |阅读模式
用hello world简单的代码更容易看懂托盘的具体实现,未完待续,一些细节问题,比如为什么提示消息没有显示在托盘上,如何在tip消息上增加图标等。

  1. #include <QtGui>
  2. class myclass: public QWidget
  3. {
  4. public:
  5.         myclass();
  6. private:
  7.         QPushButton *b1;
  8.         QSystemTrayIcon *trayIcon;
  9.        
  10. };

  11. myclass::myclass()
  12. {       
  13.         QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
  14.         setMinimumSize(200,100);
  15.         setMaximumSize(200,100);
  16.         b1=new QPushButton("hello world",this);
  17.         b1->setGeometry(20,20,160,40);
  18.         QIcon icon = QIcon("./images/bad.svg");
  19.         setWindowIcon(icon);
  20.         trayIcon = new QSystemTrayIcon(this);
  21.         trayIcon->setIcon(icon);
  22.         trayIcon->setToolTip("a trayicon example");
  23.         QString titlec=tr("看吧");
  24.         QString textc=QString::fromLocal8Bit("就这德行啊");
  25.         trayIcon->show();
  26.         trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
  27.         setWindowTitle(tr("Systray"));
  28. }

  29. int main(int argc,char **argv)
  30. {
  31.         QApplication testc(argc,argv);
  32.         myclass newc;
  33.         newc.show();
  34.         return testc.exec();
  35. }
复制代码
 楼主| 发表于 2008-3-12 19:16:49 | 显示全部楼层
进一步研究,加入点击托盘的响应,使提示信息显示在托盘上,未完待续托盘的右键菜单。

经验一:编译信号和槽 需要有.h .cpp这样的格式,否则不会生成相应的信号和槽;

经验二:信号和槽之间可以递参数,但是不允许函数直接给槽提供参数。

hello.h

  1. #include <QtGui>
  2. class myclass: public QWidget
  3. {
  4. Q_OBJECT
  5. public:
  6. myclass();
  7. private:
  8. QPushButton *b1;
  9. QSystemTrayIcon *trayIcon;
  10. char *msg;
  11. void showMessage(char *msg);
  12. private slots:
  13. void showM();
  14. void iconActivated(QSystemTrayIcon::ActivationReason reason);
  15. };
复制代码

hello.cpp

  1. #include <QtGui>
  2. #include "hello.h"
  3. myclass::myclass()
  4. {
  5. QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
  6. setMinimumSize(200,100);
  7. setMaximumSize(200,100);
  8. b1=new QPushButton("hello world",this);
  9. b1->setGeometry(20,20,160,40);
  10. QIcon icon = QIcon("./images/bad.svg");
  11. setWindowIcon(icon);
  12. trayIcon = new QSystemTrayIcon(this);
  13. trayIcon->setIcon(icon);
  14. trayIcon->setToolTip("a trayicon example");
  15. trayIcon->show();
  16. setWindowTitle(tr("Systray"));

  17. connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
  18.                      this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
  19. connect(b1,SIGNAL(clicked()),this,SLOT(showM()));
  20. }

  21. void myclass::showM()
  22. {
  23. QString titlec=tr("slot需要signal相同的参数进行传递");
  24. QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
  25. trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
  26. }

  27. void myclass::showMessage(char *msg)
  28. {
  29. QString titlec=tr(msg);
  30. QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
  31. trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
  32. }

  33. void myclass::iconActivated(QSystemTrayIcon::ActivationReason reason)
  34. {
  35. switch (reason) {
  36. case QSystemTrayIcon::Trigger:
  37. showMessage("单击");
  38. break;
  39. case QSystemTrayIcon::DoubleClick:
  40. showMessage("双击啦");
  41. break;
  42. case QSystemTrayIcon::MiddleClick:
  43. showMessage("你用的是三轮鼠标还是滚轮鼠标啊");
  44. break;
  45. default:
  46. ;
  47. }
  48. }

  49. int main(int argc,char **argv)
  50. {
  51. QApplication testc(argc,argv);
  52. myclass newc;
  53. newc.show();
  54. return testc.exec();
  55. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2008-3-13 17:20:01 | 显示全部楼层
那么最后我们把所有元素都结合在一起来实现这么一个托盘程序,比较上个例子我们增加了右键菜单并使它管理窗口的大小。

hello.h

  1. #include <QtGui>
  2. class myclass: public QWidget
  3. {
  4.         Q_OBJECT       
  5. public:
  6.         myclass();
  7. private:
  8.         QPushButton *b1;
  9.         QSystemTrayIcon *trayIcon;
  10.         char *msg;
  11.         void showMessage(char *msg);
  12.         void createActions();
  13.         void createTrayIcon();
  14.         QAction *minimizeAction;
  15.         QAction *maximizeAction;
  16.         QAction *restoreAction;
  17.         QAction *quitAction;
  18.         QMenu *trayIconMenu;
  19. private slots:
  20.         void showM();
  21.         void iconActivated(QSystemTrayIcon::ActivationReason reason);
  22. };

复制代码


hello.cpp

  1. #include <QtGui>
  2. #include "hello.h"
  3. myclass::myclass()
  4. {       
  5.         QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
  6.         setMinimumSize(200,100);
  7.         b1=new QPushButton("hello world",this);
  8.         b1->setGeometry(20,20,160,40);
  9.         QIcon icon = QIcon("./images/bad.svg");
  10.         setWindowIcon(icon);
  11.         trayIcon = new QSystemTrayIcon(this);
  12.         trayIcon->setIcon(icon);
  13.         trayIcon->setToolTip("a trayicon example");
  14. createActions();
  15. createTrayIcon();
  16.         trayIcon->show();
  17.         setWindowTitle(tr("Systray"));
  18. connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
  19. connect(b1,SIGNAL(clicked()),this,SLOT(showM()));
  20. }

  21. void myclass::showM()
  22. {
  23.         QString titlec=tr("slot需要signal相同的参数进行传递");
  24.         QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
  25.         trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
  26.       }
  27.                
  28. void myclass::showMessage(char *msg)
  29. {
  30.         QString titlec=tr(msg);
  31.         QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
  32.         trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
  33.       }
  34.                
  35. void myclass::iconActivated(QSystemTrayIcon::ActivationReason reason)
  36. {
  37.      switch (reason) {
  38.      case QSystemTrayIcon::Trigger:
  39. showMessage("单击");
  40.         break;
  41.      case QSystemTrayIcon::DoubleClick:
  42. showMessage("双击啦");
  43.          break;
  44.      case QSystemTrayIcon::MiddleClick:
  45. showMessage("你用的是三轮鼠标还是滚轮鼠标啊");
  46.          break;
  47.      default:
  48.          ;
  49.      }
  50. }

  51. void myclass::createActions()
  52. {
  53.      minimizeAction = new QAction(tr("最小化 (&I)"), this);
  54.      connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

  55.      maximizeAction = new QAction(tr("最大化 (&X)"), this);
  56.      connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

  57.      restoreAction = new QAction(tr("还原 (&R)"), this);
  58.      connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

  59.      quitAction = new QAction(tr("退出 (&Q)"), this);
  60.      connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
  61. }

  62. void myclass::createTrayIcon()
  63. {
  64.      trayIconMenu = new QMenu(this);
  65.      trayIconMenu->addAction(minimizeAction);
  66.      trayIconMenu->addAction(maximizeAction);
  67.      trayIconMenu->addAction(restoreAction);
  68.      trayIconMenu->addSeparator();
  69.      trayIconMenu->addAction(quitAction);
  70.      trayIcon->setContextMenu(trayIconMenu);
  71. }


  72. int main(int argc,char **argv)
  73. {
  74.         QApplication testc(argc,argv);
  75.         myclass newc;
  76.         newc.show();
  77.         return testc.exec();
  78. }

复制代码

[ 本帖最后由 haulm 于 2008-3-13 17:23 编辑 ]
回复

使用道具 举报

发表于 2009-5-15 23:59:11 | 显示全部楼层
如何实现点击Dialog的最小化就最小化到托盘呢?
回复

使用道具 举报

 楼主| 发表于 2009-5-16 10:17:53 | 显示全部楼层
通过重新定义closeEvent事件 event->ignore() 可以解决这个问题
protected:
        void closeEvent(QCloseEvent *event);

void myclass::closeEvent(QCloseEvent *event)
{
    if (this->trayIcon->isVisible()) {
       hide();
        event->ignore();
    }
}
回复

使用道具 举报

发表于 2009-5-16 13:11:06 | 显示全部楼层
谢谢haulm。

版主,您提供的代码是“点击关闭”然后最小化到托盘的吧?


我要的是“点击右上角的最小化按钮”,然后最小化到托盘。。。。
回复

使用道具 举报

发表于 2009-5-16 23:14:25 | 显示全部楼层
QWindowStateChangeEvent
回复

使用道具 举报

发表于 2009-5-17 13:06:26 | 显示全部楼层

  1. void Dialog::changeEvent(QEvent * event )
  2. {
  3.     if(event->WindowStateChange)
  4.     {
  5.            switch(this->windowState()){
  6.                case Qt::WindowMinimized:
  7.                     //this->showNormal();
  8.                     this->hide();
  9.                     event->ignore();
  10.                    // event->accept();
  11.                    break;
  12.                // case  Qt::WindowNoState:
  13.                         //this->show();
  14.                       //  event->ignore();
  15.                       //  break;
  16.                // default:
  17.                    break;
  18.                }
  19.     }
  20. }
复制代码
结果点击“最小化”按钮,状态栏还存在图标,但那个图标是“死的”了,点击它,还可以在桌面显示,但已经是白茫茫的一片。。。。
回复

使用道具 举报

发表于 2009-5-17 13:52:19 | 显示全部楼层

附详细代码

dialog.h


  1. #ifndef DIALOG_H
  2. #define DIALOG_H

  3. #include <QtGui/QDialog>
  4. #include <QSystemTrayIcon>

  5. namespace Ui
  6. {
  7.     class Dialog;
  8. }
  9. class QPaintEvent;
  10. class Dialog : public QDialog
  11. {
  12.     Q_OBJECT

  13. public:
  14.     Dialog(QWidget *parent = 0);
  15.     ~Dialog();

  16. private:
  17.     Ui::Dialog *ui;
  18.     QSystemTrayIcon *trayIcon;
  19.     QMenu *trayIconMenu;
  20.     QAction *minimizeAction;

  21.     //QAction *maximizeAction;
  22.     QAction *restoreAction;
  23.     QAction *quitAction;
  24.     char *msg;
  25.     void createActions();
  26.     void createTrayIcon();
  27.     void showMessage(char *msg);
  28. protected:
  29.         void closeEvent(QCloseEvent *event);
  30.         void changeEvent(QEvent * event );
  31.         void paintEvent(QPaintEvent *event);

  32. private slots:
  33.     void on_pushButton_clicked();
  34.     void on_pushButton2_clicked();
  35.     void showM();
  36.     void iconActivated(QSystemTrayIcon::ActivationReason reason);
  37. };

  38. #endif // DIALOG_H
复制代码
回复

使用道具 举报

发表于 2009-5-17 13:52:51 | 显示全部楼层

dialog.cpp


  1. #include <QMessageBox>
  2. #include <QtGui>
  3. #include <QtCore/QTextCodec>
  4. #include <QtGui/QCloseEvent>
  5. #include <QMenu>
  6. #include "dialog.h"
  7. #include "ui_dialog.h"

  8. Dialog::Dialog(QWidget *parent)
  9.     : QDialog(parent), ui(new Ui::Dialog)
  10. {
  11.     char *string = "你好,世界!";
  12.    // QTextCodec *codec = QTextCodec::codecForName("GBK");
  13.    // QString strText = codec->toUnicode(string);
  14.    // QString str;
  15.     //str = str.fromLocal8Bit(string);
  16.     this->setWindowFlags(Qt::WindowMinimizeButtonHint);
  17.     ui->setupUi(this);
  18.     ui->textEdit->clear();
  19.     ui->textEdit->setText(QObject::tr(string));
  20.     //QObject::connect(ui->pushButton2, SIGNAL(clicked()),this, SLOT(close()));
  21.     //QObject::connect(ui->pushButton, SIGNAL(clicked()),this, SLOT(on_pushButton_clicked()));
  22.     QIcon icon = QIcon("./hello.svg");
  23.     setWindowIcon(icon);
  24.     //this->restoreAction = new QAction(tr("还原 (&R)"), this);
  25.     trayIcon = new QSystemTrayIcon(this);
  26.     trayIcon->setIcon(icon);
  27.     trayIcon->setToolTip("1st trip of QT4");
  28.     QString titlec=QObject::tr("QT4");
  29.     QString textc=QObject::tr("初次约会");
  30.     trayIcon->show();
  31.     createActions();
  32.     createTrayIcon();
  33.     trayIcon->show();
  34.     trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,50);
  35.     setWindowTitle(tr("QT4"));
  36.     connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));


  37.      connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

  38. }


  39. Dialog::~Dialog()
  40. {
  41.     delete ui;
  42. }

  43. void Dialog::on_pushButton2_clicked()
  44. {
  45.     this->close();
  46. }

  47. void Dialog::on_pushButton_clicked()
  48. {
  49.    QMessageBox::information(this,tr("朋友"),tr("您好"));

  50. }

  51. void Dialog::showM()
  52. {
  53.         QString titlec=tr("双击打开主窗体");
  54.         QString textc=QString::fromLocal8Bit("右键选择其他操作");
  55.         trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
  56.       }

  57. void Dialog::showMessage(char *msg)
  58. {
  59.         QString titlec=tr(msg);
  60.         QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
  61.        trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);

  62. }

  63. void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
  64. {
  65.      switch (reason) {
  66.      case QSystemTrayIcon::Trigger:
  67.              //showMessage("单击");
  68.             // this->showM();
  69.              //connect(this->restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
  70.               break;
  71.      case QSystemTrayIcon::DoubleClick:
  72.             this->showNormal();
  73.             //showMessage("双击啦");
  74.            break;
  75.      //case QSystemTrayIcon::MiddleClick:
  76.             //showMessage("你用的是三轮鼠标还是滚轮鼠标啊");
  77.            // this->showM();
  78.             break;
  79.      default:
  80.            ;
  81.      }
  82. }

  83. void Dialog::createActions()
  84. {
  85.      minimizeAction = new QAction(tr("最小化 (&I)"), this);
  86.      connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

  87.      //maximizeAction = new QAction(tr("最大化 (&X)"), this);
  88.      //connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

  89.      restoreAction = new QAction(tr("还原 (&R)"), this);
  90.      connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

  91.      quitAction = new QAction(tr("退出 (&Q)"), this);
  92.      connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
  93. }

  94. void Dialog::createTrayIcon()
  95. {
  96.      trayIconMenu = new QMenu(this);
  97.      trayIconMenu->addAction(minimizeAction);
  98.      //trayIconMenu->addAction(maximizeAction);
  99.      trayIconMenu->addAction(restoreAction);
  100.      trayIconMenu->addSeparator();
  101.      trayIconMenu->addAction(quitAction);
  102.      trayIcon->setContextMenu(trayIconMenu);
  103. }
  104. void Dialog::closeEvent(QCloseEvent *event)
  105. {
  106.     if (this->trayIcon->isVisible())
  107.     {
  108.       QMessageBox::information(this, tr("QT4"),
  109.               tr("程序会一直运行在系统托盘。您可以通过右键点击程序对应的系统托盘图标,然后选择菜单中"退出"选项来终止程序的运行。"));
  110.        hide();
  111.        event->ignore();
  112.     }
  113. }
  114. void Dialog::changeEvent(QEvent * event )
  115. {
  116.     if(event->WindowStateChange)
  117.     {
  118.            switch(this->windowState()){
  119.                case Qt::WindowMinimized:
  120.                     //this->showNormal();
  121.                     this->hide();
  122.                     event->ignore();
  123.                    // event->accept();
  124.                    break;
  125.                // case  Qt::WindowNoState:
  126.                         //this->show();
  127.                       //  event->ignore();
  128.                       //  break;
  129.                // default:
  130.                    break;
  131.                }
  132.     }
  133. }
  134. void Dialog::paintEvent(QPaintEvent *event)
  135. {
  136.      QPainter painter;
  137.      painter.begin(this);
  138.      painter.setRenderHint(QPainter::Antialiasing);
  139.      //helper->paint(&painter, event, elapsed);
  140.      painter.end();
  141. }

复制代码
回复

使用道具 举报

发表于 2009-5-17 13:53:45 | 显示全部楼层

ui_dialog.h


  1. /********************************************************************************
  2. ** Form generated from reading ui file 'dialog.ui'
  3. **
  4. ** Created: Sat May 16 22:52:04 2009
  5. **      by: Qt User Interface Compiler version 4.5.0
  6. **
  7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
  8. ********************************************************************************/

  9. #ifndef UI_DIALOG_H
  10. #define UI_DIALOG_H

  11. #include <QtCore/QVariant>
  12. #include <QtGui/QAction>
  13. #include <QtGui/QApplication>
  14. #include <QtGui/QButtonGroup>
  15. #include <QtGui/QDialog>
  16. #include <QtGui/QHBoxLayout>
  17. #include <QtGui/QHeaderView>
  18. #include <QtGui/QPushButton>
  19. #include <QtGui/QTextEdit>
  20. #include <QtGui/QVBoxLayout>
  21. #include <QtGui/QWidget>

  22. QT_BEGIN_NAMESPACE

  23. class Ui_Dialog
  24. {
  25. public:
  26.     QWidget *widget;
  27.     QVBoxLayout *verticalLayout;
  28.     QTextEdit *textEdit;
  29.     QHBoxLayout *horizontalLayout;
  30.     QPushButton *pushButton;
  31.     QPushButton *pushButton2;

  32.     void setupUi(QDialog *Dialog)
  33.     {
  34.         if (Dialog->objectName().isEmpty())
  35.             Dialog->setObjectName(QString::fromUtf8("Dialog"));
  36.         Dialog->resize(303, 276);
  37.         widget = new QWidget(Dialog);
  38.         widget->setObjectName(QString::fromUtf8("widget"));
  39.         widget->setGeometry(QRect(20, 40, 258, 227));
  40.         verticalLayout = new QVBoxLayout(widget);
  41.         verticalLayout->setSpacing(6);
  42.         verticalLayout->setMargin(11);
  43.         verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
  44.         verticalLayout->setContentsMargins(0, 0, 0, 0);
  45.         textEdit = new QTextEdit(widget);
  46.         textEdit->setObjectName(QString::fromUtf8("textEdit"));

  47.         verticalLayout->addWidget(textEdit);

  48.         horizontalLayout = new QHBoxLayout();
  49.         horizontalLayout->setSpacing(6);
  50.         horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
  51.         pushButton = new QPushButton(widget);
  52.         pushButton->setObjectName(QString::fromUtf8("pushButton"));

  53.         horizontalLayout->addWidget(pushButton);

  54.         pushButton2 = new QPushButton(widget);
  55.         pushButton2->setObjectName(QString::fromUtf8("pushButton2"));

  56.         horizontalLayout->addWidget(pushButton2);


  57.         verticalLayout->addLayout(horizontalLayout);

  58.         QWidget::setTabOrder(textEdit, pushButton);
  59.         QWidget::setTabOrder(pushButton, pushButton2);

  60.         retranslateUi(Dialog);

  61.         QMetaObject::connectSlotsByName(Dialog);
  62.     } // setupUi

  63.     void retranslateUi(QDialog *Dialog)
  64.     {
  65.         Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
  66.         textEdit->setHtml(QApplication::translate("Dialog", "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">\n"
  67. "<html><head><meta name="qrichtext" content="1" /><style type="text/css">\n"
  68. "p, li { white-space: pre-wrap; }\n"
  69. "</style></head><body style=" font-family:'\345\256\213\344\275\223'; font-size:9pt; font-weight:400; font-style:normal;">\n"
  70. "<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">ffffffffff</p></body></html>", 0, QApplication::UnicodeUTF8));
  71.         pushButton->setText(QApplication::translate("Dialog", "\346\202\250\345\245\275", 0, QApplication::UnicodeUTF8));
  72.         pushButton2->setText(QApplication::translate("Dialog", "\347\246\273\345\274\200", 0, QApplication::UnicodeUTF8));
  73.         Q_UNUSED(Dialog);
  74.     } // retranslateUi

  75. };

  76. namespace Ui {
  77.     class Dialog: public Ui_Dialog {};
  78. } // namespace Ui

  79. QT_END_NAMESPACE

  80. #endif // UI_DIALOG_H

复制代码
回复

使用道具 举报

发表于 2009-5-17 13:54:21 | 显示全部楼层

main.cpp


  1. #include <QtGui/QApplication>
  2. #include <QtCore/QTextCodec>
  3. #include <QMessageBox>
  4. #include "dialog.h"

  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication a(argc, argv);
  8.     QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
  9.     Dialog w;
  10.     w.show();
  11.     return a.exec();
  12. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2009-5-17 14:57:38 | 显示全部楼层
我也是从例子中去学习的,很少有程序去实现最小化按钮功用定义,如果要美观可以搞个图标按钮在上面。我之所以这么说是因为你想实现的最小化到托盘的确是比较少的做法。所以看到你的提问我没有深入去研究。
WindowStateChange应该是不能用event->ignore();来中断的,event->ignore();是为了防止closeEvent程序就此关闭了。建议你去找找最小化的事件,如果系统的确没提供就用一个图标按钮来实现好了。
顺便提一下,别把UI生成的代码放上来,离开UI它不能编译,你有试过QHideEvent么?
回复

使用道具 举报

 楼主| 发表于 2009-5-17 15:17:04 | 显示全部楼层
我在linux下运行以下代码是正常的,麻烦管理员把多余的代码回帖删掉吧
void Window::hideEvent(QHideEvent *event)
{
    if (trayIcon->isVisible()) {
        hide();
        event->ignore();
    }
}
回复

使用道具 举报

发表于 2009-5-17 18:10:30 | 显示全部楼层
先对版主表示感谢。


没有试过QHideEvent哦

只有ui_dialog.h是UI生成的代码。发出来是供大家参考,如果有兴趣。

  1. void Window::hideEvent(QHideEvent *event)
  2. {
  3.     if (trayIcon->isVisible()) {
  4.         hide();
  5.         event->ignore();
  6.     }
  7. }
复制代码
在我这里也正常啊,没有问题。


请注意:这里有2个最小化,一是:点击右上角的最小化按钮;二是,点击托盘菜单中的最小化按钮。

实现第二没有问题,就是参考lz的代码。

就是实现第一个,没有成功。

[ 本帖最后由 hover_sky 于 2009-5-17 18:14 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-3-29 09:21 , Processed in 0.137703 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表