📄 qsystemtrayicon.cpp
字号:
\fn void QSystemTrayIcon::activated(QSystemTrayIcon::ActivationReason reason) This signal is emitted when the user activates the system tray icon. \a reason specifies the reason for activation. QSystemTrayIcon::ActivationReason enumerates the various reasons. \sa QSystemTrayIcon::ActivationReason*//*! \fn void QSystemTrayIcon::messageClicked() This signal is emitted when the message displayed using showMessage() was clicked by the user. \sa activated()*//*! Returns true if the system tray is available; otherwise returns false. If the system tray is currently unavailable but becomes available later, QSystemTrayIcon will automatically add an entry in the system tray if it is \l visible.*/bool QSystemTrayIcon::isSystemTrayAvailable(){ return QSystemTrayIconPrivate::isSystemTrayAvailable_sys();}/*! Returns true if the system tray supports balloon messages; otherwise returns false. \sa showMessage()*/bool QSystemTrayIcon::supportsMessages(){#if defined(Q_WS_QWS) return false;#endif return true;}/*! \fn void QSystemTrayIcon::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint) \since 4.3 Shows a balloon message for the entry with the given \a title, \a message and \a icon for the time specified in \a millisecondsTimeoutHint. Message can be clicked by the user; the messageClicked() signal will emitted when this occurs. Note that display of messages are dependent on the system configuration and user preferences, and that messages may not appear at all. Hence, it should not be relied upon as the sole means for providing critical information. On Windows, the \a millisecondsTimeoutHint is usually ignored by the system when the application has focus. \sa show() supportsMessages() */void QSystemTrayIcon::showMessage(const QString& title, const QString& msg, QSystemTrayIcon::MessageIcon icon, int msecs){ Q_D(QSystemTrayIcon); if (d->visible) d->showMessage_sys(title, msg, icon, msecs);}//////////////////////////////////////////////////////////////////////static QBalloonTip *theSolitaryBalloonTip = 0;void QBalloonTip::showBalloon(QSystemTrayIcon::MessageIcon icon, const QString& title, const QString& message, QSystemTrayIcon *trayIcon, const QPoint& pos, int timeout, bool showArrow){ hideBalloon(); theSolitaryBalloonTip = new QBalloonTip(icon, title, message, trayIcon); if (timeout < 0) timeout = 10000; //10 s default theSolitaryBalloonTip->balloon(pos, timeout, showArrow);}void QBalloonTip::hideBalloon(){ if (!theSolitaryBalloonTip) return; theSolitaryBalloonTip->hide(); delete theSolitaryBalloonTip; theSolitaryBalloonTip = 0;}QBalloonTip::QBalloonTip(QSystemTrayIcon::MessageIcon icon, const QString& title, const QString& message, QSystemTrayIcon *ti) : QWidget(0, Qt::ToolTip), trayIcon(ti), timerId(-1){ setAttribute(Qt::WA_DeleteOnClose); QObject::connect(ti, SIGNAL(destroyed()), this, SLOT(close())); QLabel *titleLabel = new QLabel; titleLabel->installEventFilter(this); titleLabel->setText(title); QFont f = titleLabel->font(); f.setBold(true); titleLabel->setFont(f); titleLabel->setTextFormat(Qt::PlainText); // to maintain compat with windows QPushButton *closeButton = new QPushButton; closeButton->setIcon(style()->standardIcon(QStyle::SP_DockWidgetCloseButton)); closeButton->setIconSize(QSize(18, 18)); closeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); closeButton->setFixedSize(18, 18); QObject::connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); QLabel *msgLabel = new QLabel; msgLabel->installEventFilter(this); msgLabel->setText(message); msgLabel->setTextFormat(Qt::PlainText); msgLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft); // smart size for the message label int limit = QApplication::desktop()->availableGeometry(msgLabel).size().width() / 3; if (msgLabel->sizeHint().width() > limit) { msgLabel->setWordWrap(true); if (msgLabel->sizeHint().width() > limit) { msgLabel->d_func()->ensureTextControl(); if (QTextControl *control = msgLabel->d_func()->control) { QTextOption opt = control->document()->defaultTextOption(); opt.setWrapMode(QTextOption::WrapAnywhere); control->document()->setDefaultTextOption(opt); } } msgLabel->setFixedSize(limit, msgLabel->heightForWidth(limit)); } QIcon si; switch (icon) { case QSystemTrayIcon::Warning: si = style()->standardIcon(QStyle::SP_MessageBoxWarning); break; case QSystemTrayIcon::Critical: si = style()->standardIcon(QStyle::SP_MessageBoxCritical); break; case QSystemTrayIcon::Information: si = style()->standardIcon(QStyle::SP_MessageBoxInformation); break; case QSystemTrayIcon::NoIcon: default: break; } QGridLayout *layout = new QGridLayout; if (!si.isNull()) { QLabel *iconLabel = new QLabel; iconLabel->setPixmap(si.pixmap(15, 15)); iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); iconLabel->setMargin(2); layout->addWidget(iconLabel, 0, 0); layout->addWidget(titleLabel, 0, 1); } else { layout->addWidget(titleLabel, 0, 0, 1, 2); } layout->addWidget(closeButton, 0, 2); layout->addWidget(msgLabel, 1, 0, 1, 3); layout->setSizeConstraint(QLayout::SetFixedSize); layout->setMargin(3); setLayout(layout); QPalette pal = palette(); pal.setColor(QPalette::Window, QColor(0xff, 0xff, 0xe1)); setPalette(pal);}QBalloonTip::~QBalloonTip(){ theSolitaryBalloonTip = 0;}void QBalloonTip::paintEvent(QPaintEvent *){ QPainter painter(this); painter.drawPixmap(rect(), pixmap);}void QBalloonTip::resizeEvent(QResizeEvent *ev){ QWidget::resizeEvent(ev);}void QBalloonTip::balloon(const QPoint& pos, int msecs, bool showArrow){ QRect scr = QApplication::desktop()->screenGeometry(pos); QSize sh = sizeHint(); const int border = 1; const int ah = 18, ao = 18, aw = 18, rc = 7; bool arrowAtTop = (pos.y() + sh.height() + ah < scr.height()); bool arrowAtLeft = (pos.x() + sh.width() - ao < scr.width()); setContentsMargins(border + 3, border + (arrowAtTop ? ah : 0) + 2, border + 3, border + (arrowAtTop ? 0 : ah) + 2); updateGeometry(); sh = sizeHint(); int ml, mr, mt, mb; QSize sz = sizeHint(); if (!arrowAtTop) { ml = mt = 0; mr = sz.width() - 1; mb = sz.height() - ah - 1; } else { ml = 0; mt = ah; mr = sz.width() - 1; mb = sz.height() - 1; } QPainterPath path; path.moveTo(ml + rc, mt); if (arrowAtTop && arrowAtLeft) { if (showArrow) { path.lineTo(ml + ao, mt); path.lineTo(ml + ao, mt - ah); path.lineTo(ml + ao + aw, mt); } move(qMax(pos.x() - ao, scr.left() + 2), pos.y()); } else if (arrowAtTop && !arrowAtLeft) { if (showArrow) { path.lineTo(mr - ao - aw, mt); path.lineTo(mr - ao, mt - ah); path.lineTo(mr - ao, mt); } move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2), pos.y()); } path.lineTo(mr - rc, mt); path.arcTo(QRect(mr - rc*2, mt, rc*2, rc*2), 90, -90); path.lineTo(mr, mb - rc); path.arcTo(QRect(mr - rc*2, mb - rc*2, rc*2, rc*2), 0, -90); if (!arrowAtTop && !arrowAtLeft) { if (showArrow) { path.lineTo(mr - ao, mb); path.lineTo(mr - ao, mb + ah); path.lineTo(mr - ao - aw, mb); } move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2), pos.y() - sh.height()); } else if (!arrowAtTop && arrowAtLeft) { if (showArrow) { path.lineTo(ao + aw, mb); path.lineTo(ao, mb + ah); path.lineTo(ao, mb); } move(qMax(pos.x() - ao, scr.x() + 2), pos.y() - sh.height()); } path.lineTo(ml + rc, mb); path.arcTo(QRect(ml, mb - rc*2, rc*2, rc*2), -90, -90); path.lineTo(ml, mt + rc); path.arcTo(QRect(ml, mt, rc*2, rc*2), 180, -90); // Set the mask QBitmap bitmap = QBitmap(sizeHint()); bitmap.fill(Qt::color0); QPainter painter1(&bitmap); painter1.setPen(QPen(Qt::color1, border)); painter1.setBrush(QBrush(Qt::color1)); painter1.drawPath(path); setMask(bitmap); // Draw the border pixmap = QPixmap(sz); QPainter painter2(&pixmap); painter2.setPen(QPen(Qt::black, border)); painter2.setBrush(palette().color(QPalette::Window)); painter2.drawPath(path); if (msecs > 0) timerId = startTimer(msecs); show();}void QBalloonTip::mousePressEvent(QMouseEvent *e){ close(); if(e->button() == Qt::LeftButton) emit trayIcon->messageClicked();}void QBalloonTip::timerEvent(QTimerEvent *e){ if (e->timerId() == timerId) { killTimer(timerId); if (!underMouse()) close(); return; } QWidget::timerEvent(e);}void qtsystray_sendActivated(QSystemTrayIcon *i, int r){ emit i->activated((QSystemTrayIcon::ActivationReason)r);}#endif // QT_NO_SYSTEMTRAYICON
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -