Qt menu QMenu and menu bar QMenuBar and custom menu usage

This article mainly introduces the usage of Qt menu QMenu, menu bar QMenuBar and custom menu. The article introduces the sample code in great detail, which has certain reference learning value for everyone's study or work. Friends who need it, follow the editor to learn together.

Table of contents

QMenu and QMenuBar are menu classes and menu bar classes in Qt, where the menu QMenu is mounted on the menu bar QMenuBar. This article mainly summarizes three common usages of QMenu, which are conventional usage, custom menu item usage inherited from QWidgetAction, and usage of QMenu as a QWidget to mount a layout.

The following is a principle explanation and code summary for each usage.

The benefits of this article, the fee to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc. )

1. Conventional usage

1.1 Principle explanation

QMenuBar is the menu bar, QMenu is the menu, and the menu QMenu is mounted on the menu bar QMenuBar, which is equivalent to the menu bar QMenuBar is a container, and the menu QMenu is one of the items in the container, or all the subsets in the QMenuBar are menu QMenu, as shown in the following figure:

 

1.2 Add general menu bar and menu usage code

#include <QMenu>
#include <QAction>
#include <QMenuBar>
#include <QVBoxLayout>
 
void Widget::on_pushButton_clicked()
{
    QMenu *menu=new QMenu("菜单:常规");
    QAction *action1=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action1");
    QAction *action2=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action2");
    QList<QAction*> list;
    list.append(action1);
    list.append(action2);
    menu->addActions(list);
    QMenuBar *menuBar=new QMenuBar;
    menuBar->addMenu(menu);
    menuBar->addSeparator();    //分隔栏
    ui->verticalLayout->addWidget(menuBar);
}

 

2. Inherit QWidgetAction custom menu item usage

2.1 Principle explanation

When we need to customize each item in the menu, that is, the content of the menu items that need to be customized. A menu item is a subset of the menu, as shown in the following figure:

 

At this time, the action we want to add is preferably a QWidget, so that it can be arranged arbitrarily. Qt provides such a class QWidgetAction. This class inherits from QAction, so for any need to mount QAction and want to customize the interface, QWidgetAction can be used instead of QAction to customize the interface.

There are two ways to call QWidgetAction, one is directly used as QAction, but when creating an object, the parent control must be associated with QMenu or this; after calling, set the function void QWidgetAction::setDefaultWidget(QWidget *w); set a custom QWidget interface, and then use the menu to add QWidgetAction objects like adding QAction. Another way is to inherit QWidgetAction, and design the QWidget layout to be defined in the inherited subclass constructor. The following code also uses the method of inheriting QWidgetAction.

2.2 Code example

Add two classes QMyMenu and QMyWidgetAction respectively, where QMyMenu inherits from QMenu, and QMyWidgetAction inherits from QWidgetAction.

qmywidgetaction.h

#ifndef QMYWIDGETACTION_H
#define QMYWIDGETACTION_H
 
#include <QWidgetAction>
 
class QMyWidgetAction : public QWidgetAction
{    
public:
    explicit QMyWidgetAction(QWidget *parent=0);
 
protected:
    virtual QWidget *createWidget(QWidget *parent);
};
 
#endif // QMYWIDGETACTION_H

qmywidgetaction.cpp

#include "qmywidgetaction.h"
#include <QPushButton>
#include <QSplitter>
#include <QLabel>
#include <QLineEdit>
#include <QPixmap>
#include <QMouseEvent>
#include <QHBoxLayout>
#include <QDebug>
 
QMyWidgetAction::QMyWidgetAction(QWidget *parent):QWidgetAction(parent)
{
}
 
QWidget *QMyWidgetAction::createWidget(QWidget *parent)
{
    QLabel* lab = new QLabel("label1");
    QPushButton *button1=new QPushButton("button1");
    QHBoxLayout *hlayout=new QHBoxLayout;
    hlayout->setMargin(0);
    hlayout->setSpacing(0);
    hlayout->addWidget(lab);
    hlayout->addWidget(button1);
    QWidget* widget = new QWidget(parent); //如果写成 QSplitter* sp = new QSplitter;  就无法显示!!!
    widget->setLayout(hlayout);
    connect(button1,&QPushButton::clicked,[this](bool){qDebug()<<"单击按钮!";});
    return widget;
}

qmymenu.h

#ifndef QMYMENU_H
#define QMYMENU_H
 
#include <QMenu>
#include <QMouseEvent>
 
class QMyMenu : public QMenu
{
public:
    explicit QMyMenu(const QString &title, QWidget *parent = Q_NULLPTR);
 
protected:
};
 
#endif // QMYMENU_H

qmymenu.cpp

#include "qmymenu.h"
#include <QAction>
 
QMyMenu::QMyMenu(const QString &title, QWidget *parent):QMenu(title,parent) //初始化子类构造函数
{
 
}

Call the following code in the button slot function

#include "qmywidgetaction.h"
#include "qmymenu.h"
#include <QAction>
#include <QMenuBar>
#include <QVBoxLayout>
 
void Widget::on_pushButton_3_clicked()
{
    QMyMenu *menu= new QMyMenu("菜单:继承QWidgetAction用法");
    QAction *action1=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"1");   //设置图标和内容
    menu->addAction(action1);
    QMyWidgetAction *myWidgetAction=new QMyWidgetAction(menu);
    menu->addSeparator();   //添加分割线
    menu->addAction(myWidgetAction);
    QMenuBar *menuBar=new QMenuBar(this);
    menuBar->setStyleSheet("QMenuBar{background-color:red}"
                           "QMenuBar:hover{background-color:blue}");
    menuBar->addMenu(menu);
    ui->verticalLayout->addWidget(menuBar);
    ui->verticalLayout->addStretch();
}

The result is shown in the figure below

 

3. Use QMenu as a QWidget to mount a layout usage

3.1 Detailed explanation of the principle

QMenu inherits QWidget, so we can make layout call as QWidget, and add our custom interface on the layout.

class Q_WIDGETS_EXPORT QMenu : public QWidget
{}

3.2 sample code

void Widget::on_pushButton_2_clicked()
{
    QMenu *menu=new QMenu("菜单:当Widget用,添加一个布局");
    QAction *action1=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action1");
    QAction *action2=new QAction(QIcon(":/resource/image/向右箭头.jpg"),"action2");
    QList<QAction*> list;
    list.append(action1);
    list.append(action2);
    QVBoxLayout *vlayout=new QVBoxLayout;
    vlayout->setContentsMargins(10,0,10,0);
    QPushButton *button1=new QPushButton("11");
    vlayout->addWidget(button1);
    menu->setLayout(vlayout);
    QMenuBar *menuBar=new QMenuBar;
    menuBar->addMenu(menu);
    menuBar->addSeparator();
    ui->verticalLayout->addWidget(menuBar);
}

The result is shown in the figure below:

 

Summary: The specific usage is determined by business requirements. However, if it is a custom menu item, I tend to use the third one. It is the easiest and most convenient to use when QMenu is used as a QWidget, and it is easy to make various QSS style sheet effects.

Original address: Qt menu QMenu and menu bar QMenuBar and custom menu usage_C language_Programming Home

The benefits of this article, the fee to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc. )

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/131870848