• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            zhonghua

            C++博客 首頁 新隨筆 聯系 聚合 管理
              72 Posts :: 1 Stories :: 4 Comments :: 0 Trackbacks

            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());
                }
            }

            posted on 2012-06-01 16:25 米米 閱讀(913) 評論(0)  編輯 收藏 引用 所屬分類: qt
            久久精品综合一区二区三区| 国产亚洲精久久久久久无码| 99久久综合国产精品二区| 精品少妇人妻av无码久久| 久久这里只有精品首页| 理论片午午伦夜理片久久| 婷婷综合久久中文字幕蜜桃三电影 | 999久久久无码国产精品| 51久久夜色精品国产| 狠狠色丁香久久婷婷综合_中| 久久精品人成免费| 久久强奷乱码老熟女网站| 无码人妻精品一区二区三区久久久| 国产亚洲欧美成人久久片| 午夜视频久久久久一区 | 久久久久99精品成人片| 狠狠色噜噜色狠狠狠综合久久| 狠色狠色狠狠色综合久久| 少妇人妻综合久久中文字幕| 久久香蕉一级毛片| 少妇精品久久久一区二区三区| 国产综合成人久久大片91| 狠狠色婷婷久久一区二区三区| 性做久久久久久久久老女人| 日本三级久久网| 久久国产高清字幕中文| 午夜欧美精品久久久久久久| 无码任你躁久久久久久老妇| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 69国产成人综合久久精品| 久久久久久久97| 最新久久免费视频| 久久亚洲欧洲国产综合| 韩国三级中文字幕hd久久精品| 99久久综合狠狠综合久久止| 国产aⅴ激情无码久久| 久久精品极品盛宴观看| 久久久精品波多野结衣| 久久高清一级毛片| 久久国产香蕉一区精品| 国内精品久久久久久久涩爱|