QT 实现上下文菜单
来源:互联网 发布:sybase数据库win7 编辑:程序博客网 时间:2024/05/21 23:32
一.重载contextMenuEvent()函数,一个简单的示例如下:
void MainWindow::contextMenuEvent(QContextMenuEvent*event)
{
filemenu->addAction(newAction);
filemenu->addAction(editAction);
filemenu->addAction(delAction);
filemenu->exec(QCursor::pos()); //位于鼠标点击处
//filemenu->exec(this->mapToGlobal(QPoint(0,0)));//位于父组件的(0,0)坐标处
//event->accept();//有时候需要加上这句,因为许多情况下默认为ignore事件
}
二.在类的构造函数中进行相关设置,如我们可以定义如下函数,再在构造函数中调用它
void MainWindow::createContextMenu()
{
addAction(newAction);
addAction(editAction);
addAction(delAction);
setContextMenuPolicy(Qt::ActionsContextMenu);
}
其中
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().
今天又在书上看到这么一段话,刚好可以用在这里,首先来看一下这段话:
Creating modal dialogs and context menus in QWidget::contextMenuEvent() reimplementations on the stack is a common programming pattern since we usually don't need the dialog or menu after we have used it,and it will automatically be destroyed at the end of the enclosing scope.
根据这段话所说,我们要对程序进行修改的话,只需要把QMenu与QAction的实例化放在相应的函数里面,可以直接用一个变量,也可以用new,不过用new的时候要记得在函数结尾处要调用delete。这个比较简单,就不在这里列出相应的代码了。
转自:http://blog.chinaunix.net/uid-12890544-id-2911697.html
- QT 实现上下文菜单
- QT中实现上下文菜单
- QT中实现上下文菜单
- QT中实现上下文菜单
- Qt 上下文菜单
- Qt 创建上下文菜单
- MFC 实现上下文菜单
- Android 上下文菜单实现
- Android 上下文菜单实现
- Android 上下文菜单实现
- Qt学习—qt上下文菜单显示
- Flex上下文菜单的实现
- ExpandableListView上下文菜单的实现
- android 实现上下文菜单实例
- MFC添加上下文菜单,实现菜单功能
- 【Android】菜单功能的实现:上下文菜单
- 通过Fragment实现简单的上下文菜单
- 长按listview弹出上下文菜单实现
- Activity与Intent
- Dns服务的简单配置
- Windows Phone 下载网络图片
- WPF进阶之接口(4):ICommand实现详解
- Java反射详解
- QT 实现上下文菜单
- Java 执行对象构造函数理解
- 电脑常识
- 二进制注册表数据应该怎样读取?
- web中翻页功能的实现
- MyEclipse中几个小的技巧
- Oracle中的几个问题记录
- jfreechart学习
- 传智播客学习日记]ADO.Net连接与增删改查