Both qwidget and its subclass can have a right-click menu, because qwidget has two functions related to the Right-click menu:
Qt: contextmenupolicy () const
Void setcontextmenupolicy (QT: contextmenupolicy Policy)
Qt: contextmenupolicy enumeration types include QT: defaultcontextmenu, QT: nocontextmenu, QT: preventcontextmenu, QT: actionscontextmenu, and QT: customcontextmenu.
The usage is as follows:
1) The default value is QT: defaultcontextmenu.
It uses the context menu event contextmenuevent () to process (which means the contextmenuevent () handler is called ). It is to override the contextmenuevent (qcontextmenuevent * event) function.
2) use QT: customcontextmenu.
It sends a qwidget: customcontextmenurequested signal. Note that it only sends a signal, which means you need to write the slot that displays the right-click menu by yourself.
This signal is the only signal related to the Right-click menu of qwidget (which is also its own unique signal) and is also easily ignored signal:
Void customcontextmenurequested (const qpoint & Pos)
The condition for sending this signal is that the user requests the contextmenu (usually right-click the mouse) and the widget whose contextmenupolicy is also the QT: customcontextmenu.
Note: POS is the position where the widget receives context menu events, which is generally in the coordinate system of the widget. However, the exception of qabstratscrollarea and its subclass is the coordinate system corresponding to its viewport () in the viewport. For example, common qtableview and qheaderview are subclasses of qabstratscrollarea.
Because only signals are sent, you need to write the slot that displays the right-click menu to respond. For example, the right-click menu slot for displaying a table (qtableview type) header:
Datatable-> horizontalheader ()-> setcontextmenupolicy (QT: customcontextmenu );
Connect (datatable-> horizontalheader (), signal (customcontextmenurequested (const qpoint &)),
This, slot (show_contextmenu (const qpoint &); // This is the window where the datatable is located
Qmenu * cmenu = NULL;
Show_contextmenu (const qpoint & Pos)
{
If (cmenu) // ensure that only one menu exists at the same time and the memory is released in time
{
Delete cmenu;
Cmenu = NULL;
}
Qmenu cmenu = new qmenu (datatable-> horizontalheader ());
Qaction * ascendsortaction = cmenu-> addaction ("ASCENDING ");
Qaction * descendsortaction = cmenu-> addaction ("descending ");
Qaction * filteraction = cmenu-> addaction ("filter ");
Qaction * reshowaction = cmenu-> addaction ("overload ");
Connect (ascendsortaction, signal (triggered (bool), this, slot (sort_ascend ()));
Connect (descendsortaction, signal (triggered (bool), this, slot (sort_descend ()));
Connect (filteraction, signal (triggered (bool), this, slot (show_filter_dlg ()));
Connect (reshowaction, signal (triggered (bool), this, slot (reshow_data ()));
Cmenu-> exec (qcursor: pos (); // displayed at the current mouse position
// Cmenu-> exec (POS) is displayed in viewport
}
You can also do a good job of cmenu first. The advantage is to always use one:
Qmenu cmenu = new qmenu (datatable-> horizontalheader ());
Qaction * ascendsortaction = cmenu-> addaction ("ASCENDING ");
Qaction * descendsortaction = cmenu-> addaction ("descending ");
Qaction * filteraction = cmenu-> addaction ("filter ");
Qaction * reshowaction = cmenu-> addaction ("overload ");
Connect (ascendsortaction, signal (triggered (bool), this, slot (sort_ascend ()));
Connect (descendsortaction, signal (triggered (bool), this, slot (sort_descend ()));
Connect (filteraction, signal (triggered (bool), this, slot (show_filter_dlg ()));
Connect (reshowaction, signal (triggered (bool), this, slot (reshow_data ()));
Show_contextmenu (const qpoint & Pos)
{
If (cmenu)
{
Cmenu-> exec (qcursor: pos ());
}
}
3) Use QT: actionscontextmenu.
Display all the actions of the component, namely, qwidget: Actions (), as the context menu.
In the preceding example, You need to right-click the table (qtableview type) header to display the menu:
Qaction * ascendsortaction = new qaction ("ASCENDING", this );
Qaction * descendsortaction = new qaction ("descending order", this );
Qaction * filteraction = new qaction ("filter", this );
Qaction * unfilteraction = new qaction ("cancel filtering", this );
Connect (ascendsortaction, signal (triggered (bool), this, slot (sort_ascend ()));
Connect (descendsortaction, signal (triggered (bool), this, slot (sort_descend ()));
Connect (filteraction, signal (triggered (bool), this, slot (filter_table ()));
Connect (unfilteraction, signal (triggered (bool), this, slot (unfilter_table ()));
Datatable-> horizontalheader ()-> addaction (ascendsortaction );
Datatable-> horizontalheader ()-> addaction (descendsortaction );
Datatable-> horizontalheader ()-> addaction (filteraction );
Datatable-> horizontalheader ()-> addaction (unfilteraction );
Datatable-> horizontalheader ()-> setcontextmenupolicy (QT: actionscontextmenu );
The other two do not display the context menu:
Qt: nocontextmenu
The widget does not feature a context menu, context menu handling is deferred to the widget's parent.
Qt: preventcontextmenu
The widget does not feature a context menu, and in contrast to nocontextmenu, the handling is not deferred to the widget's parent. this means that all right mouse button events are guaranteed to be delivered to the widget itself through mousepressevent (), and mousereleaseevent ().
Supplement:
The use of QT: actionscontextmenu is concise, but if you need to define different menus based on the position of the current menu pop-up, or, as in the previous example, when the right-click menu is displayed in the table (qtableview type) header, I need to know which list header is clicked, so that different sorting policies can be performed based on different columns when the sort_ascend () sorting function is called later, so QT: actionscontextmenu cannot be implemented.
To capture the pop-up position, you have to use QT: actionscontextmenu. The customcontextmenurequested (const qpoint & Pos) signal returns the click position pos (position in the header's viewport coordinate system ), then, the table header can call the logicalindexat (POS) function to obtain the index corresponding to the clicked section, that is, the column number of the clicked part, and save it for the sorting slot activated by the subsequent actions.
Show_contextmenu (const qpoint & Pos)
{
// Get related column of headerview
Contextmenu_column = datatable-> horizontalheader ()-> logicalindexat (POS );
// Show contextmenu
If (cmenu)
{
Cmenu-> exec (qcursor: pos ());
}
}
From: http://hi.baidu.com/qinpanke/blog/item/679eb78ef30313e7f01f36c6.html
From: http://www.cppblog.com/biao/archive/2010/01/01/104593.html