QT5自定义菜单

自定义菜单包括右键菜单,按钮菜单和系统托盘菜单


一:右键菜单
textEdit的右键菜单,通过槽函数on_textEdit_customContextMenuRequested()实现,其中的Action是已经创建好的
void MainWindow::on_textEdit_customContextMenuRequested()
{
    QMenu *menu = new QMenu(this);
    menu->addAction(ui->undoAction);
    menu->addAction(ui->redoAction);
    menu->addSeparator();
    menu->addAction(ui->cutAction);
    menu->addAction(ui->copyAction);
    menu->addAction(ui->pasteAction);
    menu->addSeparator();
    menu->addAction(ui->selectAllAction);
    menu->exec(QCursor::pos());
}
效果 QT5自定义菜单_第1张图片

参考: Qt之自定义菜单(右键菜单)


二:托盘菜单
void MainWindow::createSystemTrayIcon()
{
    trayIcon=new QSystemTrayIcon(this);//创建托盘
    trayIcon->setIcon(QIcon(tr(":images/icon")));//设置托盘图标
    trayIcon->setToolTip(tr("托盘"));

    play_widget = new QWidget();
    play_widget_Action = new QWidgetAction(this);

    /*********创建所需的按钮和动作并连接信号槽********/
    play_Button = new QPushButton();
    play_Button ->setCursor(Qt::PointingHandCursor);
    play_Button ->setFlat(true);
    play_Button->setFixedSize(40,40);
    connect(play_Button,SIGNAL(clicked()), this, SLOT(Play()));

    last_Button = new QPushButton();
    last_Button ->setCursor(Qt::PointingHandCursor);
    last_Button ->setFlat(true);
    last_Button->setFixedSize(30,30);
    connect(last_Button, SIGNAL(clicked()), this, SLOT(Last()));

    next_Button = new QPushButton();
    next_Button->setCursor(Qt::PointingHandCursor);
    next_Button ->setFlat(true);
    next_Button->setFixedSize(30,30);
    connect(next_Button, SIGNAL(clicked()), this, SLOT(Next()));

    musicname_Label = new QLabel();
    musicname_Label->setFixedWidth(100);
    musicname_Label->setAlignment(Qt::AlignCenter);

    aboutAction = new QAction(tr("关  于"),this);
    aboutAction->setIcon(QIcon(":/images/about"));
    connect(aboutAction,SIGNAL(triggered()),this,SLOT(about()));

    quitAction=new QAction(tr("退  出"), this);
    quitAction->setIcon(QIcon(":/images/quit"));
    connect(quitAction, SIGNAL(triggered()), this, SLOT(quit()));

//使用样式表修饰按钮
    last_Button->setStyleSheet("QPushButton {border-image:url(:/images/last);}QPushButton:hover{border-image:url(:/images/last_on);}QPushButton:hover:pressed{border-image:url(:/images/last);}");
    play_Button->setStyleSheet("QPushButton {border-image:url(:/images/play);}QPushButton:hover{border-image:url(:/images/play_on);}QPushButton:hover:pressed{border-image:url(:/images/play);}");
    next_Button->setStyleSheet("QPushButton {border-image:url(:/images/next);}QPushButton:hover{border-image:url(:/images/next_on);}QPushButton:hover:pressed{border-image:url(:/images/next);}");

//将next,last和play按钮水平布局
    QHBoxLayout *Hlayout = new QHBoxLayout();
    Hlayout->addWidget(last_Button);
    Hlayout->addWidget(play_Button);
    Hlayout->addWidget(next_Button);
    Hlayout->setSpacing(0);
    Hlayout->setContentsMargins(20, 0, 20, 0);

//将上面布好的一排按钮和歌名Label垂直布局
    QVBoxLayout *Vlayout = new QVBoxLayout();
    Vlayout->addLayout(Hlayout);
    Vlayout->addWidget(musicname_Label,0,Qt::AlignCenter);
    Vlayout->setSpacing(5);
    Vlayout->setContentsMargins(0, 0, 0, 5);
    musicname_Label->setEnabled(false);

//将上面整合好的控件作为一个widget并将此widget赋给一个Action
    play_widget->setLayout(Vlayout);
    play_widget_Action->setDefaultWidget(play_widget);

//new一个托盘菜单并将上面的Action和其他Action赋给托盘菜单
    trayContextMenu=new QMenu(this);
    trayContextMenu->addAction(play_widget_Action);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(aboutAction);
    trayContextMenu->addAction(quitAction);

//将托盘菜单赋给托盘并显示托盘
    trayIcon->setContextMenu(trayContextMenu);
    trayIcon->show();

//点击托盘执行的事件
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
    this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
效果 QT5自定义菜单_第2张图片

参考: Qt之自定义菜单(托盘菜单)


三:按钮菜单

按钮菜单请参考:Qt之自定义菜单(按钮菜单)

你可能感兴趣的:(qt,qt5)