b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Qt浅谈之三十系统托盘(QSystemTrayIcon)

电脑杂谈  发布时间:2020-03-09 15:01:17  来源:网络整理

outlook最小化到托盘_qt语音在托盘怎么关掉_qt 最小化到托盘

Qt 浅谈之三十系统托盘(QSystemTrayIcon)一、简介Qt 自带的事例/usr/lib64/qt4/examples/desktop/systray 中详细介绍了平台托 盘的用途, 在其基础上进行拓宽,定制适合自己的平台托盘。托盘菜单实现:QSystemTrayIcon+QMenu+QActionqt 最小化到托盘, 有时需要加入 QWidgetAction。二、详解 1、代码 (1)systemtray.h[html] view plain copy #ifndef SYSTEMTRAY_H #define SYSTEMTRAY_H #include <QSystemTrayIcon> #include <QMenu> #include <QAction> #include <QApplication> #include <QWidgetAction> #include <QtGui> SystemTray : public QSystemTrayIcon { public: classQ_OBJECTexplicit SystemTray(QWidget *parent = 0); void setParentWidget(QWidget *parent); void~SystemTray();void setMiddleClickText(const QString &tips); setHideText(const QString &text); setToolTips(const QString &tips); void voidstartHideTips();private:void createActions(); private slots: voidvoid createTopWidget();slotActivated(QSystemTrayIcon::ActivationReason reason); void showParentWidget(); void slotClicked(); QAction *showAction; QAction *quitAction; QString middleText; hideText; *topWidget; private: void slotHelpAction(); QMenu *trayIconMenu;QAction *helpAction; QWidget *parentWidget; QString toolTils; QStringQMenu *funMenu;QWidgetQWidgetAction *topWidgetAction; QPushButton ( 2)QPushButton *showButton; *hideButton; };#endif // SYSTEMTRAY_Hsystemtray.cpp[html] view plain copy #include <QUrl> #include <QDebug> #include <QDesktopServices> #include "systemtray.h" *parent) middleText("") { SystemTray::SystemTray(QWidget ,: QSystemTrayIcon(parent) , toolTils(""), hideText("") createActions();parentWidget = parent;connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); SystemTray::~SystemTray() { } void }SystemTray::setParentWidget(QWidget *parent) { parentWidget = parent; } voidSystemTray::setMiddleClickText(const QString &tips) { middleText = tips; } voidSystemTray::setHideText(const QString &text) { hideText = text; } voidSystemTray::setToolTips(const QString &tips) { this->setToolTip(tips); } voidSystemTray::startHideTips() { this->showMessage(tr("提示信息:"), hideText, voidQSystemTrayIcon::Information, 2000); } SystemTray::createActions() { this->setIcon(QIcon(":/tray.png"));trayIconMenu = new QMenu(); funMenu = new QMenu(); QAction(this);createTopWidget(); showAction = newhelpAction = new QAction(this);quitAction = new QAction(this); funMenu->setIcon(QIcon(":/fun.png")); showAction->setIcon(QIcon(":/show.png")); helpAction->setIcon(QIcon(":/help.png")); quitAction->setIcon(QIcon(":/quit.png")); funMenu->setTitle(tr("功能"));showAction->setText(tr("显示")); helpAction->setText(tr("帮助")); quitAction->setText(tr("退出")); trayIconMenu->addAction(topWidgetAction); //trayIconMenu->setFixedWidth(250); trayIconMenu->addAction(showAction); funMenu->addAction(helpAction); trayIconMenu->addMenu(funMenu); trayIconMenu->addSeparator(); trayIconMenu->addAction(quitAction); this->setContextMenu(trayIconMenu); connect(showAction, SIGNAL(triggered()), this, SLOT(showParentWidget())); connect(helpAction,SIGNAL(triggered()), this, SLOT(slotHelpAction())); connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); } { void SystemTray::createTopWidget()topWidget = new QWidget();topWidget->setStyleSheet("QWidget{background:#2FC77B; color:#000000}QPushButton:hover{color:#FFFFFF}QPushButt on:disabled{color:#666666;border-style:flat;}"); topWidgetAction = new QWidgetAction(trayIconMenu); QLabel *titleLabel = new QLabel();titleLabel->setText(tr("系统托盘显示")); = new QPushButton(); "));showButtonshowButton->setText(tr("显示showButton->setFocusPolicy(Qt::NoFocus);hideButton = new QPushButton(); hideButton->setText(tr("隐藏")); hideButton->setFocusPolicy(Qt::NoFocus); showButton->setCursor(Qt::PointingHandCursor); hideButton->setCursor(Qt::PointingHandCursor); QHBoxLayout *hLayout = new QHBoxLayout(); hLayout->addWidget(showButton); hLayout->addStretch(); hLayout->addWidget(hideButton); hLayout->setSpacing(10); hLayout->setContentsMargins(0, 0, 0, 0); QVBoxLayout *vLayout = new QVBoxLayout(); vLayout->addWidget(titleLabel); vLayout->addLayout(hLayout); vLayout->setSpacing(10); vLayout->setContentsMargins(10, 5, 10, 5); topWidget->setLayout(vLayout); topWidgetAction->setDefaultWidget(topWidget); connect(showButton, SIGNAL(clicked()), this,SLOT(slotClicked()));connect(hideButton, } voidSIGNAL(clicked()), this, SLOT(slotClicked()));SystemTray::slotActivated(QSystemTrayIcon::ActivationReaso n reason) { (reason) { if (parentWidget) { switchcase QSystemTrayIcon::Trigger: ifcase QSystemTrayIcon::DoubleClick: { (parentWidget->isHidden() || parentWidget->windowState() == Qt::WindowMinimized) {//showNormal(),showMinimized(),showMaxmized(),showFullScreen() parentWidget->showNormal(); parentWidget->raise(); parentWidget->activateWindow(); else { startHideTips(); break; } parentWidget->hide(); } case if }QSystemTrayIcon::MiddleClick: { (!middleText.isEmpty()) {this->showMessage(tr("提示信息:"), middleText, QSystemTrayIcon::Information, 3000); break; } } caseQSystemTrayIcon::Context: { (parentWidget->isHidden() ||ifparentWidget->windowState() == Qt::WindowMinimized) { ")); showButton->setEnabled(true); hideButton->setEnabled(false); else { 藏")); showButton->setEnabled(false); hideButton->setEnabled(true); break; break; } } } } void if default: } } showAction->setText(tr("显示showAction->setText(tr("隐SystemTray::showParentWidget() { (showAction->text() == tr("显示")) {parentWidget->showNormal();parentWidget->raise(); parentWidget->activateWindow(); { parentWidget->hide(); } } void const QUrl //qDebug() } elsestartHideTips();SystemTray::slotHelpAction() {AuthorUrl(";);<< AuthorUrl.scheme(); AuthorUrl.port();//qDebug() <<QDesktopServices::openUrl(AuthorUrl); SystemTray::slotClicked() {}voidQPushButton *button = if (buttonqobject_cast<QPushButton *>(sender()); == showButton) { parentWidget->raise(); parentWidget->activateWindow(); { parentWidget->hide(); } } }parentWidget->showNormal();elsestartHideTips();(3)widget.h[html] view plain copy #ifndef WIDGET_H #define WIDGET_H #include <QtGui> #include "systemtray.h" { class Widget : public QWidget public: Widget(QWidget protected: private: #endif // WIDGET_H (4 ) voidQ_OBJECT*parent = 0);~Widget();closeEvent(QCloseEvent *event); SystemTray *systemTray; }; widget.cpp[html] view plain copy #include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent,Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint){ 400);setWindowTitle(tr("系统托盘"));resize(400,move((QApplication::desktop()->width() (QApplication::desktop()->height() systemTray = new SystemTray(this);width())/2, height())/2);systemTray->setMiddleClickText(tr("系统托盘")); systemTray->setToolTips(tr("应用程序系统托盘")); systemTray->setHideText(tr("应用程序隐藏在系统托盘")); systemTray->show(); } Widget::~Widget() { if }void Widget::closeEvent(QCloseEvent *event) { (systemTray->isVisible()) { hide(); main.cpp systemTray->startHideTips(); event->ignore(); } }( 5)[html] view plain copy #include "widget.h" #include <QApplication> { int main(int argc, char *argv[]) QTextCodec *codecQApplication a(argc, argv);= QTextCodec::codecForName("utf8"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); if(!QSystemTrayIcon::isSystemTrayAvailable()){QMessageBox::critical(NULL, QObject::tr(" return return托盘"), QObject::tr("不支持系统托盘")); 1; } Widget w; w.show();a.exec(); } 2、运行效果图四、总结 (1)注:系统托盘在 centos 下能够显示 QAction 的界面, 并且提醒 message 出现后鼠标滑过 message 在要求时间内不 会消失。(2)SystemTray 是一个对 QSystemTrayIcon 的封装,可以直 接获得其它地方使用,但切记父类对象的传递。(3)源码已经打包上传到 csdn 上(源码中有些问题在 SystemTray::createActions()函数中,可对照文章修改) ,可登 录下载 () 。(4)若有建议qt 最小化到托盘,请留言,在此先谢谢!


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-143563-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...