1. 程式人生 > >Qt右鍵選單的新增

Qt右鍵選單的新增

滑鼠事件是學習Qt必不可少的一個事件,初學者總是會忽略這一點,而右鍵選單在平常的使用中更是常見,下面就Qt中新增右鍵選單的幾種方法做一簡單的介紹:
1、滑鼠事件新增

/**重寫滑鼠處理器*/
void QWidget::mousePressEvent(QMouseEvent * event) 或者
void QWidget::mouseReleaseEvent(QMouseEvent * event)

當視窗接收到訊號是判斷滑鼠的哪一個鍵被按下:

    /**定義選單和要新增的事件*/
    QMenu                   *m_pwebMenu;
    QAction                 *m_pActionDelete;
    QAction                 *m_pActionResend;
/**初始化Menu和Action*/
m_pwebMenu = new QMenu( this );
m_pActionDelete = new QAction( tr( "DELETE" ), this );
m_pActionResend = new QAction( tr( "RESEND" ), this );
/**將Action新增到Menu中*/
m_pwebMenu->addAction( m_pActionResend );
m_pwebMenu->addAction( m_pActionDelete );
/**顯示選單*/
m_pwebMenu->exec( QCursor::pos() );

上述方法顯示右鍵選單是使用的是螢幕的座標,使用QCursor類的靜態函式pos()可以快速的定位滑鼠按下時的座標。
2、使用和右鍵選單有關的setContextMenuPolicy()函式:
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)為QWidget的成員函式,從QWidget直接或間接派生的類都可以使用該函式對右鍵選單進行設定。
Qt::ContextMenuPolicy是一個列舉型別,包括:
Qt::NoContextMenu、Qt::PreventContextMenu、Qt::DefaultContextMenu、Qt::ActionsContextMenu、Qt::CustomContextMenu
其中Qt::NoContextMenu、Qt::PreventContextMenu 不能實現右鍵選單的功能。
l Qt::DefaultContextMenu


設定此屬性需要重寫

void QWidget::contextMenuEvent(QContextMenuEvent * event)

事件處理器函式來實現右鍵選單

void Widget::contextMenuEvent(QContextMenuEvent *ev)
{
    /**初始化Menu和Action*/
    m_pwebMenu = new QMenu( this );
    m_pActionDelete = new QAction( tr( "DELETE" ), this );
    m_pActionResend = new QAction( tr( "RESEND" ), this );
    /**將Action新增到Menu中*/
    m_pwebMenu->addAction( m_pActionResend );
    m_pwebMenu->addAction( m_pActionDelete );
    /**顯示選單*/
    m_pwebMenu->exec( QCursor::pos() );
}

l Qt::ActionsContextMenu
設定此屬性後新增到當前視窗中所有QAction都會作為右鍵選單項顯示出來
在視窗建構函式中設定右鍵選單的處理方式:

setContextMenuPolicy(Qt:: ActionsContextMenu)

在當前視窗中新增QAction:

    /**初始化Menu和Action*/
    m_pwebMenu = new QMenu( this );
    m_pActionDelete = new QAction( tr( "DELETE" ), this );
    m_pActionResend = new QAction( tr( "RESEND" ), this );
    /**將Action新增到Menu中*/
    m_pwebMenu->addAction( m_pActionResend );
    m_pwebMenu->addAction( m_pActionDelete );
    /**顯示選單*/
    m_pwebMenu->exec( QCursor::pos() );

l Qt:: CustomContextMenu
它是發出QWidget::customContextMenuRequested訊號,注意僅僅只是發訊號,意味著要自己寫顯示右鍵選單的槽函式(slot)這個訊號是QWidget唯一與右鍵選單有關的訊號(也是自有的唯一訊號),同時也是很容易被忽略的signal:
void customContextMenuRequested ( const QPoint & pos )
該訊號的發出條件是:使用者請求contextMenu(常規就是滑鼠右擊)且同時被擊的widget其contextMenuPolicy又是Qt::CustomContextMenu。
注意: 訊號中的引數pos為當前視窗的座標,並非螢幕座標。

m_ui.webView->setContextMenuPolicy(Qt::CustomContextMenu);
connect( m_ui.webView, SIGNAL(customContextMenuRequested(QPoint)), 
SLOT( slot_webViewRightClicked( QPoint ) ) );
void chatdemo::slot_webViewRightClicked(const QPoint &pos)
{
    m_pwebMenu = new QMenu( this );
    m_pActionDelete = new QAction( tr( "DELETE" ), this );
    m_pActionResend = new QAction( tr( "RESEND" ), this );
    m_pwebMenu->addAction( m_pActionResend );
    m_pwebMenu->addAction( m_pActionDelete );
    m_pwebMenu->exec( QCursor::pos() );
}