在QT中QSystemTrayIcon類提供了創建系統托盤程序的功能。
QSystemTrayIcon類為系統托盤中的應用程序提供圖標。
現代操作系統通常會在桌面上提供一個稱為系統托盤(system tray)或通知(notification)區域的特殊區域,其中長時間運行的應用程序可以顯示圖標和短消息。
QSystemTrayIcon類可以在以下平台上使用:
- 所有受支持的Windows版本。
- X11的所有窗口管理器和獨立托盤實現,實現了XEmbed系統托盤規范。
- 所有實現D-Bus的X11桌面環境規范,包括最新版本的KDE和Unity。
- 所有受支持的macOS版本。
要檢查用戶桌面上是否存在系統托盤,請調用QSystemTrayIcon :: isSystemTrayAvailable()靜態函數。
要添加系統托盤條目,請創建QSystemTrayIcon對象,調用setContextMenu()以提供圖標的上下文菜單,並調用show()以使其在系統托盤中可見。可以使用showMessage()隨時顯示狀態通知消息(“氣球消息”)。
如果在構建系統托盤圖標時系統托盤不可用,但稍后可用,則QSystemTrayIcon將自動為系統托盤中的應用程序添加條目(如果圖標可見)。
當用戶激活圖標時,會發出activate()信號。
僅在X11上,當請求工具提示時,QSystemTrayIcon接收QEvent ::ToolTip類型的QHelpEvent。此外,QSystemTrayIcon接收QEvent :: Wheel類型的wheel事件。任何其他平台都不支持這些。
QT自帶一個例子systray,可學習一下!
該程序是一個基於QDialog的應用程序,只有一個繼承於QDialog的Window類。
主要代碼
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
int
main(
int
argc,
char
*argv[])
{ // Initializes the resources specified by the .qrc file with the specified base name. Normally, when resources are built as part of the application, the resources are loaded automatically at startup.
// The Q_INIT_RESOURCE() macro is necessary on some platforms for resources stored in a static library. Q_INIT_RESOURCE(systray); QApplication app(argc, argv); // Returns true if the system tray is available; otherwise returns false. if (!QSystemTrayIcon::isSystemTrayAvailable()) { QMessageBox::critical( 0 , QObject::tr( "Systray" ), QObject::tr( "I couldn't detect any system tray on this system." )); return 1 ; } QApplication::setQuitOnLastWindowClosed( false ); Window window; window.show(); return app.exec(); } Window::Window() { // Tray Icon Group Box(采用HBox布局) createIconGroupBox(); // Ballon Message Group Box(采用Grid布局) createMessageGroupBox(); iconLabel->setMinimumWidth(durationLabel->sizeHint().width()); // Tray right button menu createActions(); // Create Tray Icon createTrayIcon(); // 信號與槽的連接 connect(showMessageButton, &QAbstractButton::clicked, this , &Window::showMessage); connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible); connect(iconComboBox, QOverload< int >::of(&QComboBox::currentIndexChanged), this , &Window::setIcon); // Tray 信號槽 connect(trayIcon, &QSystemTrayIcon::messageClicked, this , &Window::messageClicked); connect(trayIcon, &QSystemTrayIcon::activated, this , &Window::iconActivated); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(iconGroupBox); mainLayout->addWidget(messageGroupBox); setLayout(mainLayout); iconComboBox->setCurrentIndex( 1 ); trayIcon->show(); setWindowTitle(tr( "Systray" )); resize( 400 , 300 ); } 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); } // 重寫closeEvent實現關閉時隱藏到托盤 void Window::closeEvent(QCloseEvent *event) { #ifdef Q_OS_OSX if (!event->spontaneous() || !isVisible()) { return ; } #endif if (trayIcon->isVisible()) { QMessageBox::information( this , tr( "Systray" ), tr( "The program will keep running in the " "system tray. To terminate the program, " "choose <b>Quit</b> in the context menu " "of the system tray entry." )); hide(); event->ignore(); } } // 重寫setVisible實現右鍵菜單的動態變化 void Window::setVisible( bool visible) { minimizeAction->setEnabled(visible); maximizeAction->setEnabled(!isMaximized()); restoreAction->setEnabled(isMaximized() || !visible); QDialog::setVisible(visible); } // 單擊或雙擊托盤圖標動態改變圖標,點擊滾輪顯示氣泡信息 void Window::iconActivated(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::Trigger: case QSystemTrayIcon::DoubleClick: iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1 ) % iconComboBox->count()); break ; case QSystemTrayIcon::MiddleClick: showMessage(); break ; default : break ; } } void Window::showMessage() { showIconCheckBox->setChecked( true ); QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(typeComboBox->itemData(typeComboBox->currentIndex()).toInt()); if (msgIcon == QSystemTrayIcon::NoIcon) { QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex())); trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, durationSpinBox->value() * 1000 ); } else { trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon, durationSpinBox->value() * 1000 ); } } |