• <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++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              72 Posts :: 1 Stories :: 4 Comments :: 0 Trackbacks

            QWidget及其子類都可有右鍵菜單,因?yàn)镼Widget有以下兩個(gè)與右鍵菜單有關(guān)的函數(shù):

            Qt::ContextMenuPolicy contextMenuPolicy () const

            void setContextMenuPolicy ( Qt::ContextMenuPolicy policy )

            Qt::ContextMenuPolicy枚舉類型包括:Qt::DefaultContextMenu, Qt::NoContextMenu, Qt::PreventContextMenu, Qt::ActionsContextMenu, and Qt::CustomContextMenu。

            使用方式如下:


            1)默認(rèn)是Qt::DefaultContextMenu。
            它 是利用右鍵菜單事件contextMenuEvent()來處理(which means the contextMenuEvent() handler is called)。就是要重寫contextMenuEvent( QContextMenuEvent * event )函數(shù)。


            2)使用Qt::CustomContextMenu。
            它是發(fā)出QWidget::customContextMenuRequested信號(hào),注意僅僅只是發(fā)信號(hào),意味著要自己寫顯示右鍵菜單的slot。
            這個(gè)信號(hào)是QWidget唯一與右鍵菜單有關(guān)的信號(hào)(也是自有的唯一信號(hào)),同時(shí)也是很容易被忽略的signal:

            void customContextMenuRequested ( const QPoint & pos )

            該信號(hào)的發(fā)出條件是:用戶請(qǐng)求contextMenu(常規(guī)就是鼠標(biāo)右擊啦)且同時(shí)被擊的widget其contextMenuPolicy又是Qt::CustomContextMenu。
            注 意:pos是該widget接收右鍵菜單事件的位置,一般是在該部件的坐標(biāo)系中。但是對(duì)于QAbstratScrollArea及其子類例外,是對(duì)應(yīng)著其 視口viewport()的坐標(biāo)系。如常用的QTableView、QHeaderView就是QAbstratScrollArea的子類。
            因?yàn)閮H發(fā)信號(hào),所以需自己寫顯示右鍵菜單的slot來響應(yīng),例如一個(gè)表格(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)//保證同時(shí)只存在一個(gè)menu,及時(shí)釋放內(nèi)存
                {
                    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());//在當(dāng)前鼠標(biāo)位置顯示
                //cmenu->exec(pos)是在viewport顯示
            }

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

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

            補(bǔ)充:
                使用Qt::ActionsContextMenu比較簡(jiǎn)潔,但是如果需要根據(jù)當(dāng)前菜單彈出的位置來定義不同菜單,或者像上個(gè)例子,在表格 (QTableView類型)表頭顯示右鍵菜單時(shí),我需要知道是哪一列表頭被點(diǎn)擊,從而在后來調(diào)用sort_ascend()排序函數(shù)時(shí)能夠根據(jù)不同列進(jìn) 行不同排序策略,那么Qt::ActionsContextMenu就做不到了。
                這種需要捕捉彈出位置的情況只好用Qt::ActionsContextMenu了,customContextMenuRequested ( const QPoint & pos )信號(hào)返回點(diǎn)擊位置pos(在表頭視口坐標(biāo)系中位置),然后表頭即可調(diào)用logicalIndexAt(pos)函數(shù)得到被點(diǎn)擊section對(duì)應(yīng)的 index即被點(diǎn)擊部分的列號(hào),然后存下來可供后面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 米米 閱讀(914) 評(píng)論(0)  編輯 收藏 引用 所屬分類: qt
            国产精品一区二区久久精品无码 | 久久亚洲中文字幕精品一区| segui久久国产精品| 久久精品国产亚洲AV不卡| 欧美日韩中文字幕久久久不卡| 欧美午夜A∨大片久久 | 人人狠狠综合久久亚洲| 亚洲国产精品无码久久青草| 久久久久久精品久久久久| 精品久久久久中文字幕日本| 久久免费国产精品一区二区| 色婷婷狠狠久久综合五月| 亚洲国产欧洲综合997久久| 久久精品国产91久久麻豆自制| 国产亚洲美女精品久久久| 久久久国产精华液| 亚洲国产成人久久精品影视| 久久无码专区国产精品发布| 狠狠狠色丁香婷婷综合久久五月| 久久最新免费视频| 国产国产成人精品久久| 亚洲第一永久AV网站久久精品男人的天堂AV| 久久综合亚洲鲁鲁五月天| 国产精品美女久久久久AV福利| 久久天天躁狠狠躁夜夜网站 | 一本色综合网久久| 国产99久久久国产精品~~牛 | 日本久久久久久中文字幕| 色婷婷久久久SWAG精品| 中文字幕亚洲综合久久2| 亚洲精品乱码久久久久久久久久久久 | 伊人久久精品影院| 久久精品人妻一区二区三区| 国产精品久久久久影院色| 亚洲中文字幕无码久久精品1 | 久久成人小视频| 热久久国产欧美一区二区精品 | 久久福利青草精品资源站免费| 99久久99久久精品国产片果冻| 欧美精品福利视频一区二区三区久久久精品 | 精品无码久久久久国产|