QT学习之窗口右键菜单
QT学习之窗口右键菜单
QWidget
及其子类都有右键菜单,主要因为其有两个与右键菜单相关联的函数:
Qt::ContextMenuPolicy contextMenuPlicy() const
void setContextMenuPolicy( Qt::ContextMenuPolicy policy)
可以看到这里使用的是一个枚举类型:
Constant | Value | Description |
---|---|---|
Qt::NoContextMenu | 0 | the widget does not feature a context menu, context menu handling is deferred to the widget's parent. |
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() . |
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::NoContextMenu
没有右键菜单,右键点击功能传递给widget的父窗口处理。
Qt::DefaultContextMenu
这个是默认的。利用右键菜单事件 QWidget::contextMenuEvent()
来处理右键事件,所以需要重写此函数!
Qt::ActionsContextMenu
通过动作事件添加右键菜单,(要给动作绑定信号槽!)
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("右键菜单显示"));
//为窗口添加QActions
addAction(new QAction(tr("&Open"), this));
addAction(new QAction(QIcon(":/images/Opt.png"), tr("&Opt"), this));
addAction(new QAction(tr("&Quit"), this));
//设置contextMenuPolicy属性值为 '以Actions为弹出菜单的菜单项组成菜单
setContextMenuPolicy(Qt::ActionsContextMenu);
}
QAction *ascendSortAction = new QAction("升序", this);
QAction *descendSortAction = new QAction("降序", this);
QAction *filterAction = new QAction("过滤", this);
QAction *unfilterAction = new QAction("取消过滤", 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);
Qt::CustomContextMenu
这个枚举意味着会发出一个信号:
void QWidget::customContextMenuRequested(const QPoint & pos) [signal]
但其只是发送信号,所以要自己去写槽函数slot。信号发出的条件为:
用户鼠标右击且被击中的Widget的contextMenuPolicy
又是Qt::CustomContextMenu
。
pos是该widget接收右键菜单事件的位置,一般是在该部件的坐标系中。但是对于QAbstratScrollArea及其子类例外,是对应着其视口viewport()的坐标系。如常用的QTableView、QHeaderView就是QAbstratScrollArea的子类。
因为仅发信号,所以需自己写显示右键菜单的slot来响应。
例如窗口显示右键菜单槽:
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
popMenu = new QMenu(this); //popMenu为类私有成员
QAction *addDir = popMenu->addAction("增加目录");
connect(addDir, SIGNAL(triggered(bool)), this, SLOT(popAddDir()));
QAction *addTemplate = popMenu->addAction("增加模板");
connect(addTemplate, SIGNAL(triggered(bool)), this, SLOT(popAddTemplate()));
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(sltShowPopMenu(const QPoint&)));
}
void MyWidget::sltShowPopMenu(const QPoint& )//槽函数
{
if(popMenu){
popMenu->exec(QCursor::pos());
}
}
void QWidget::contextMenuEvent ( QContextMenuEvent * event ) [virtual protected]
重写 QWidget 的被保护的虚函数
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("Context Menu Show 2"));
setContextMenuPolicy(Qt::DefaultContextMenu); //其实不用设置,默认就是这个值
}
void MyWidget::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = new QMenu(this);
menu->addAction(new QAction(tr("&Open"), menu));
menu->addAction(new QAction(QIcon(":/images/mark.png"), tr("&Mark"), menu));
menu->addAction(new QAction(tr("&Quit"), menu));
menu->move(cursor().pos()); //让菜单显示的位置在鼠标的坐标上
menu->show();
}
http://tmjfzy.blog.163.com/blog/static/6644702520126523645391/
http://blog.sina.com.cn/s/blog_98a4dde701013dzh.html
http://blog.csdn.net/addfourliu/article/details/7164923
作者:Lomper
出处:http://www.cnblogs.com/lomper
关于作者:小菜鸟一枚,欢迎大神指点!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.