QMenu的个性化定制

    经常使用菜单,菜单的定制相当重要,普通的样式设置不难,一般需求足以实现(QMenu + QAction)。如果要足够个性,则需要进行一定的定制。

    说起定制,其实也是利用Qt中现成的组件进行组装而已,使用QMenu + QWidgetAction + QAction ,再加上QSS美化就可实现个性的QMenu。

先看效果(模仿酷狗所做的一个托盘菜单):
QMenu的个性化定制 QMenu的个性化定制

主要代码如下:

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);
        backward_button->setToolTip(tr("backward"));
        forward_button->setToolTip(tr("forward"));

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->setIcon(play_pixmap);
play_button->setIconSize(play_pixmap.size());
play_button->setObjectName("transparentButton");

QPixmap  backward_pixmap(":/action/backward");
        backward_button->setIcon(backward_pixmap);
backward_button->setIconSize(backward_pixmap.size());
backward_button->setObjectName("transparentButton");

QPixmap forward_pixmap(":/action/forward");
        forward_button->setIcon(forward_pixmap);
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);
}

 

你可能感兴趣的:(menu)