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