Qt:QT右鍵選單

pamxy發表於2013-07-25

轉自:http://www.cppblog.com/biao/archive/2010/01/01/104593.html

QWidget及其子類都可有右鍵選單,因為QWidget有以下兩個與右鍵選單有關的函式:

Qt::ContextMenuPolicy contextMenuPolicy () const

void setContextMenuPolicy ( Qt::ContextMenuPolicy policy )

Qt::ContextMenuPolicy列舉型別包括:Qt::DefaultContextMenu, Qt::NoContextMenu, Qt::PreventContextMenu, Qt::ActionsContextMenu, and Qt::CustomContextMenu。

使用方式如下:


1)預設是Qt::DefaultContextMenu。
它是利用右鍵選單事件contextMenuEvent()來處理(which means the contextMenuEvent() handler is called)。就是要重寫contextMenuEvent( QContextMenuEvent * event )函式。


2)使用Qt::CustomContextMenu。
它是發出QWidget::customContextMenuRequested訊號,注意僅僅只是發訊號,意味著要自己寫顯示右鍵選單的slot。
這個訊號是QWidget唯一與右鍵選單有關的訊號(也是自有的唯一訊號),同時也是很容易被忽略的signal:

void customContextMenuRequested ( const QPoint & pos )

該訊號的發出條件是:使用者請求contextMenu(常規就是滑鼠右擊啦)且同時被擊的widget其contextMenuPolicy又是Qt::CustomContextMenu。
注意:pos是該widget接收右鍵選單事件的位置,一般是在該部件的座標系中。但是對於QAbstratScrollArea及其子類例外,是對應著其視口viewport()的座標系。如常用的QTableView、QHeaderView就是QAbstratScrollArea的子類。
因為僅發訊號,所以需自己寫顯示右鍵選單的slot來響應,例如一個表格(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)//保證同時只存在一個menu,及時釋放記憶體
    {
        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());//在當前滑鼠位置顯示
    //cmenu->exec(pos)是在viewport顯示
}

也可先做好cmenu,好處是始終使用一個:
    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);

另外兩個就是不顯示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().

補充:
    使用Qt::ActionsContextMenu比較簡潔,但是如果需要根據當前選單彈出的位置來定義不同選單,或者像上個例子,在表格(QTableView型別)表頭顯示右鍵選單時,我需要知道是哪一列表頭被點選,從而在後來呼叫sort_ascend()排序函式時能夠根據不同列進行不同排序策略,那麼Qt::ActionsContextMenu就做不到了。
    這種需要捕捉彈出位置的情況只好用Qt::ActionsContextMenu了,customContextMenuRequested ( const QPoint & pos )訊號返回點選位置pos(在表頭視口座標系中位置),然後表頭即可呼叫logicalIndexAt(pos)函式得到被點選section對應的index即被點選部分的列號,然後存下來可供後面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());
    }
}


From: http://hi.baidu.com/qinpanke/blog/item/679eb78ef30313e7f01f36c6.html


相關文章