一.重载contextMenuEvent()函数,一个简单的示例如下:
|
|
二.在类的构造函数中进行相关设置,如我们可以定义如下函数,再在构造函数中调用它
|
|
其中
Qt::ActionsContextMenu是位于Qt命名空间的一个枚举类型,它的定义为:
enum Qt::ContextMenuPolicy
This enum type defines the various policies a widget can have with respect to showing a context menu.
Qt::NoContextMenu 0 the widget does not feature a context menu, context menu handling is deferred to the widget's parent.
Qt::DefaultContextMenu 1 the widget's QWidget::contextMenuEvent() handler is called.
Qt::ActionsContextMenu 2 the widget displays its QWidget::actions() as context menu.
Qt::CustomContextMenu 3 the widget emits the QWidget::customContextMenuRequested() signal.
Qt::PreventContextMenu 4 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().
今天又在书上看到这么一段话,刚好可以用在这里,首先来看一下这段话:
|
|
根据这段话所说,我们要对程序进行修改的话,只需要把QMenu与QAction的实例化放在相应的函数里面,可以直接用一个变量,也可以用new,不过用new的时候要记得在函数结尾处要调用delete。这个比较简单,就不在这里列出相应的代码了。

5889

被折叠的 条评论
为什么被折叠?



