先看效果(模仿酷狗、360安全卫士所做的一个托盘菜单):
以下是模仿酷狗托盘的主要代码:
SystemTray::SystemTray()
: QSystemTrayIcon()
{
this->createActions();
this->createPlayAction();
this->addActions();
this->translateLanguage();
this->setContextMenu(pop_menu);
}
SystemTray::~SystemTray()
{
}
void SystemTray::translateLanguage()
{
this->setToolTip(tr("hey music"));
this->switchPlayToolTip(is_paly);
show_action->setText(tr("show"));
setting_action->setText(tr("setting"));
lock_lrc_action->setText(tr("lock lrc"));
this->showLrc(is_show);
login_action->setText(tr("login"));
logout_action->setText(tr("logout"));
}
void SystemTray::createActions()
{
this->setIcon(QIcon(":/images/music.png"));
pop_menu = new QMenu();
show_action = new QAction(pop_menu);
setting_action = new QAction(pop_menu);
lock_lrc_action = new QAction(pop_menu);
show_lrc_action = new QAction(pop_menu);
login_action = new QAction(pop_menu);
logout_action = new QAction(pop_menu);
connect(show_action, &QAction::triggered, this, &SystemTray::showWidget);
connect(show_lrc_action, &QAction::triggered, this, static_cast(&SystemTray::showLrc));
connect(login_action, &QAction::triggered, this, &SystemTray::showLoginWidget);
connect(logout_action, &QAction::triggered, this, &SystemTray::logoutWidget);
}
void SystemTray::createPlayAction()
{
play_widget = new QWidget();
play_widget_action = new QWidgetAction(pop_menu);
play_button = new QPushButton();
backward_button = new QPushButton();
forward_button = new QPushButton();
music_name_label = new QLabel();
music_name_label->setFixedWidth(MUSIC_NAME_WIDTH);
music_name_label->setAlignment(Qt::AlignCenter);
QPixmap play_pixmap(":/action/pause");
play_button->setIconSize(play_pixmap.size());
play_button->setObjectName("transparentButton");
QPixmap backward_pixmap(":/action/backward");
backward_button->setIconSize(backward_pixmap.size());
backward_button->setObjectName("transparentButton");
QPixmap forward_pixmap(":/action/forward");
forward_button->setIconSize(forward_pixmap.size());
forward_button->setObjectName("transparentButton");
backward_button->setCursor(Qt::PointingHandCursor);
play_button->setCursor(Qt::PointingHandCursor);
forward_button->setCursor(Qt::PointingHandCursor);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(backward_button);
layout->addWidget(play_button);
layout->addWidget(forward_button);
layout->setSpacing(0);
layout->setContentsMargins(20, 0, 20, 0);
QVBoxLayout *layout2 = new QVBoxLayout();
layout2->addLayout(layout);
layout2->addWidget(music_name_label, 0, Qt::AlignCenter);
layout2->setSpacing(5);
layout2->setContentsMargins(0, 0, 0, 5);
music_name_label->setText("player");
music_name_label->setEnabled(false);
play_widget->setLayout(layout2);
play_widget_action->setDefaultWidget(play_widget);
connect(play_button, &QPushButton::clicked, this, &SystemTray::play);
connect(backward_button, &QPushButton::clicked, this, &SystemTray::skipBackward);
connect(forward_button, &QPushButton::clicked, this, &SystemTray::skipForward);
}
void SystemTray::addActions()
{
pop_menu->addAction(play_widget_action);
pop_menu->addSeparator();
pop_menu->addAction(show_action);
pop_menu->addSeparator();
pop_menu->addAction(setting_action);
pop_menu->addAction(lock_lrc_action);
pop_menu->addAction(show_lrc_action);
pop_menu->addSeparator();
pop_menu->addAction(login_action);
pop_menu->addAction(logout_action);
}
注: