QWidget及其子類都可有右鍵菜單,因?yàn)镼Widget有以下兩個(gè)與右鍵菜單有關(guān)的函數(shù):
Qt::ContextMenuPolicy contextMenuPolicy () const
void setContextMenuPolicy ( Qt::ContextMenuPolicy policy )
Qt::ContextMenuPolicy枚舉類型包括:Qt::DefaultContextMenu, Qt::NoContextMenu, Qt::PreventContextMenu, Qt::ActionsContextMenu, and Qt::CustomContextMenu。
使用方式如下:
1)默認(rèn)是Qt::DefaultContextMenu。
它 是利用右鍵菜單事件contextMenuEvent()來處理(which means the contextMenuEvent() handler is called)。就是要重寫contextMenuEvent( QContextMenuEvent * event )函數(shù)。
2)使用Qt::CustomContextMenu。
它是發(fā)出QWidget::customContextMenuRequested信號(hào),注意僅僅只是發(fā)信號(hào),意味著要自己寫顯示右鍵菜單的slot。
這個(gè)信號(hào)是QWidget唯一與右鍵菜單有關(guān)的信號(hào)(也是自有的唯一信號(hào)),同時(shí)也是很容易被忽略的signal:
void customContextMenuRequested ( const QPoint & pos )
該信號(hào)的發(fā)出條件是:用戶請(qǐng)求contextMenu(常規(guī)就是鼠標(biāo)右擊啦)且同時(shí)被擊的widget其contextMenuPolicy又是Qt::CustomContextMenu。
注 意:pos是該widget接收右鍵菜單事件的位置,一般是在該部件的坐標(biāo)系中。但是對(duì)于QAbstratScrollArea及其子類例外,是對(duì)應(yīng)著其 視口viewport()的坐標(biāo)系。如常用的QTableView、QHeaderView就是QAbstratScrollArea的子類。
因?yàn)閮H發(fā)信號(hào),所以需自己寫顯示右鍵菜單的slot來響應(yīng),例如一個(gè)表格(QTableView類型)表頭的顯示右鍵菜單槽:
datatable->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(datatable->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(show_contextmenu(const QPoint&)));//this是datatable所在窗口
QMenu *cmenu = NULL;
show_contextmenu(const QPoint& pos)
{
if(cmenu)//保證同時(shí)只存在一個(gè)menu,及時(shí)釋放內(nèi)存
{
delete cmenu;
cmenu = NULL;
}
QMenu cmenu = new QMenu(datatable->horizontalHeader());
QAction *ascendSortAction = cmenu->addAction("升序");
QAction *descendSortAction = cmenu->addAction("降序");
QAction *filterAction = cmenu->addAction("過濾");
QAction *reshowAction = cmenu->addAction("重載");
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());//在當(dāng)前鼠標(biāo)位置顯示
//cmenu->exec(pos)是在viewport顯示
}
也可先做好cmenu,好處是始終使用一個(gè):
QMenu cmenu = new QMenu(datatable->horizontalHeader());
QAction *ascendSortAction = cmenu->addAction("升序");
QAction *descendSortAction = cmenu->addAction("降序");
QAction *filterAction = cmenu->addAction("過濾");
QAction *reshowAction = cmenu->addAction("重載");
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)使用Qt::ActionsContextMenu。
把部件的所有action即QWidget::actions()作為context menu顯示出來。
還是上面的例子,要在表格(QTableView類型)表頭顯示右鍵菜單:
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);
datatable->horizontalHeader()->setContextMenuPolicy(Qt::ActionsContextMenu);
另外兩個(gè)就是不顯示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().
補(bǔ)充:
使用Qt::ActionsContextMenu比較簡(jiǎn)潔,但是如果需要根據(jù)當(dāng)前菜單彈出的位置來定義不同菜單,或者像上個(gè)例子,在表格 (QTableView類型)表頭顯示右鍵菜單時(shí),我需要知道是哪一列表頭被點(diǎn)擊,從而在后來調(diào)用sort_ascend()排序函數(shù)時(shí)能夠根據(jù)不同列進(jìn) 行不同排序策略,那么Qt::ActionsContextMenu就做不到了。
這種需要捕捉彈出位置的情況只好用Qt::ActionsContextMenu了,customContextMenuRequested ( const QPoint & pos )信號(hào)返回點(diǎn)擊位置pos(在表頭視口坐標(biāo)系中位置),然后表頭即可調(diào)用logicalIndexAt(pos)函數(shù)得到被點(diǎn)擊section對(duì)應(yīng)的 index即被點(diǎn)擊部分的列號(hào),然后存下來可供后面action激活的排序槽使用。
show_contextmenu(const QPoint& pos)
{
//get related column of headerview
contextmenu_column = datatable->horizontalHeader()->logicalIndexAt(pos);
//show contextmenu
if(cmenu)
{
cmenu->exec(QCursor::pos());
}
}