Qt application development (basics) - Menu QMenu

I. Introduction

        The QMenu class inherits fromQWidget, which provides a menu-style widget for Menu bar, context menu and some pop-up menus.

        The QMenu menu option is optional. It can be a drop-down menu or an independent context menu. Drop-down menuUsually used when the user clicks the corresponding item or presses the specified shortcut key, use QMenuBar::addMenu() to insert the menu into In the menu bar, the menu bar will display a drop-down menu. Context menus are usually invoked via some special keyboard keys or right-click. They can be executed asynchronously using popup() or exec()Synchronized execution. Of course, the menu can also be called in some trigger events, such as when a button is pressed.

       QMenuThe menu consists of a series of operation itemsQAcion, QAcion is called in QMenu When adding addAction(), addActions(), insertAction(), the actions are expressed vertically , the style is rendered byQStyle. Additionally, actions can have a text label, an optional icon drawn on the far left, and a shortcut key sequence.

void MainWindow::on_pushButton_clicked()
{
    QMenu *menu = new QMenu();                
    menu->addAction(QIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon)),"copy");
    menu->addAction("cut");
    menu->addAction("paste");
    menu->popup(QPoint(this->pos().x()+100,this->pos().y()+100));
}

        QMenuThe menu has four types of action items:Separator, < a i=4>Submenu, Widgets, and Perform Action a>, they send signals to the QAction triggered from the menu, and we can also know which QAction was triggered from here.        hover() and triggered() to insert the sub menu. When inserting an action item, you usually specify a receiver and slot. In addition, QMenu provides two signals, addMenu() to insert the separator, use addSeparator(). Use

        In the following example, we specify the copy action and define the signal slot at the same time, and bind the triggered signal of the menu. Clicking copy will print "copy Triggered" twice.

        Warning: To make QMenu visible on software, you should use exec() or popup() instead of show(). If show() is used, the menu will be displayed directly in the upper left corner of the screen without specifying coordinates.

void MainWindow::on_pushButton_clicked()
{
QMenu *menu = new QMenu();                
QAction *copyAcy = menu->addAction(QIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon)),"copy",this,SLOT(onCopyTriggered()),QKeySequence(Qt::CTRL + Qt::Key_C));
menu->addAction("cut");
menu->addSeparator();
QMenu *sub_menu = new QMenu("help");
menu->addMenu(sub_menu);
menu->addMenu(sub_menu);
sub_menu->addAction("about");
menu->popup(QPoint(this->pos().x()+100,this->pos().y()+100));
    connect(menu,&QMenu::triggered,[=](QAction *action *action){
        if(action == copyAcy)
        {
            qDebug()<<"copy Triggered";
        }
    });
}

void MainWindow::onCopyTriggered()
{
    qDebug()<<"copy Triggered";
}

2. QMenu class

1、icon

        This attribute represents the option icon of the menu. The default is an empty icon.

bool autoRepeat() const
void setAutoRepeat(bool)

         ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ In the above example? Let’s add a sentence:

sub_menu->setIcon(QIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon)));

2、separatorsCollapsible

        This property indicates whether consecutive separators should be visually collapsed into a single separator. The separators at the beginning or end of the menu are also hidden. This property defaults to true.

separatorsCollapsible = false

       

separatorsCollapsible = true

3、tearOffEnabled

        This attribute indicates whether the menu can be torn off. When true, the menu contains a special tearable item (usually shown as a dotted line at the top of the menu). Users can "tear off" frequently used menus, which creates a copy of the menu when triggered. This "tear-off" copy is located in a separate window on the screen. This concept is usually not used on Microsoft Windows, so we generally don't use it and just treat it as an understanding. This property defaults to false.

bool isTearOffEnabled() const
void setTearOffEnabled(bool)

4、title

        This attribute represents the title of the menu. By default, it is an empty string, so it is not displayed.

QString title() const
void setTitle(const QString &title)

5、toolTipsVisible

        This attribute indicates whether to display tool tips. The default is false.

bool toolTipsVisible() const
void setToolTipsVisible(bool visible)
QAction *copyAcy = menu->addAction(QIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIIcon)),"copy",this,SLOT(onCopyTriggered()),QKeySequence(Qt::CTRL + Qt::Key_C));
copyAcy->setToolTip("this is copy");

6. Public functions

1)addAction

        Add action QAction, return QAction pointer, you can pass in text, icons, slot functions that trigger responses and key shortcuts.

QAction *addAction(const QString &text)
QAction *addAction(const QIcon &icon, const QString &text)
QAction *addAction(const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut = 0)
QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut = 0)
QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0)
QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0)
QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0)
QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0)
2) actionAt

        Returns the QAction pointer at the specified coordinate pos, or returns a null pointer if it does not exist.

QAction *actionAt(const QPoint &pt) const
3)actionGeometry

        Returns the QAction pointer at the specified coordinate pos, or returns a null pointer if it does not exist.

QRect actionGeometry(QAction *act) const
4)activeAction

        Returns the currently highlighted QAction pointer, or a null pointer if there is no currently highlighted operation.

QAction *activeAction() const
5)setActiveAction

        ​ ​ ​Set the specified QAction to highlight.

void setActiveAction(QAction *act)

6)defaultAction

        Returns the current default QAction pointer.

QAction *defaultAction() const
 7)setDefaultAction

        ​​​Return a QAction as the default action. The default action will have a visual prompt, which usually indicates what will happen by default when deletion occurs.

QAction *defaultAction() const

8) addMenu

        Add a submenu, refer to the example above.

QAction *addMenu(QMenu *menu)
QMenu *addMenu(const QString &title)
QMenu *addMenu(const QIcon &icon, const QString &title)
9)insertMenu

        ​ ​ ​ Insert the menu before before.

QAction *insertMenu(QAction *before, QMenu *menu)
10)addSection

        Added section action, the style is a separator with text and icon, and the QAction pointer is returned. It's like a QAction::setSeparator(true) but with text and icon prompts. The style depends on the system's GUI style.

QAction *addSection(const QString &text)
QAction *addSection(const QIcon &icon, const QString &text)
QMenu *menu = new QMenu();
menu->addAction(QIcon(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon)),"copy");
menu->addSection("handle");
menu->addAction("cut");
menu->addAction("paste");

 11)insertSection

        Insert seciton action before before.

QAction *insertSection(QAction *before, const QString &text)
QAction *insertSection(QAction *before, const QIcon &icon, const QString &text)
12)addSeparator

        Added delimiter.

QAction *addSeparator()
13)addSeparator

        Insert a delimiter before before.

QAction *insertSeparator(QAction *before)
14)exec

        ​ ​ ​ Execute this menu synchronously, equivalent to exec(pos()). Returns the QAction that fired in the popup menu or its submenu, or a null pointer if no item was triggered (usually because the user pressed Esc).

QAction *exec()
QAction *exec(const QPoint &p, QAction *action = nullptr)
//静态函数
QAction *exec(QList<QAction *> actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr)

        In most cases, you need to specify the location yourself, such as:

exec(QCursor::pos());
exec(somewidget.mapToGlobal(QPoint(0,0)));
exec(e->globalPos());
15)popup

        Display the menu, so that the action ataction is located in the specified global position P.

void popup(const QPoint &p, QAction *atAction = nullptr)
16)clear

        Delete the operation of all menus.

void clear()
 17)isEmpty

       Returns whether the number of menu operation items is empty.

bool isEmpty() const
 18)setAsDockMenu

        ​​​Set this menu as an available dock menu by option-clicking the application dock icon. Only available on macOS.

void setAsDockMenu()
  19)menuAction

        Returns the QAction pointer associated with this menu

QAction *menuAction() const

7. Signal

1)hovered

        This signal is triggered when the menu operation is highlighted. Action is the action that triggers the signal, usually used to update status information.

void hovered(QAction *action)
2)triggered

        This signal is fired when an action in this menu is triggered. action is the action that triggered the signal. Typically you would connect each menu action's trigger() signal to its own custom slot, but sometimes you will want to connect several actions together. Connected to a slot, for example, when you have a set of closely related actions, such as "left-aligned", "centered", "right-aligned".

void triggered(QAction *action)
3)aboutToShow

        This signal is triggered when the menu is displayed.​ 

void aboutToShow()
4)aboutToHide

        This signal is triggered when the menu is hidden.​ 

void aboutToHide()

Guess you like

Origin blog.csdn.net/u014491932/article/details/132823034