QT學習之窗口右鍵菜單

#QT學習之窗口右鍵菜單

QWidget 及其子類都有右鍵菜單,主要由於其有兩個與右鍵菜單相關聯的函數:html

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() 來處理右鍵事件,因此須要重寫此函數!ui

Qt::ActionsContextMenu

經過動做事件添加右鍵菜單,(要給動做綁定信號槽!)this

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::CustomContextMenuurl

pos是該widget接收右鍵菜單事件的位置,通常是在該部件的座標系中。可是對於QAbstratScrollArea及其子類例外,是對應着其視口viewport()的座標系。如經常使用的QTableView、QHeaderView就是QAbstratScrollArea的子類。
由於僅發信號,因此需本身寫顯示右鍵菜單的slot來響應。
.net

例如窗口顯示右鍵菜單槽:3d

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 的被保護的虛函數code

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/7164923htm

相關文章
相關標籤/搜索