国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Pyqt在QListWidget中添加右鍵菜單

2019-11-14 17:37:06
字體:
來源:轉載
供稿:網友

Pyqt 的資料奇少, 攻破難點之后, 就在這里記一下筆記.

QListWidget 是繼承 QWidget 的, 所以 QListWidget 是有右鍵菜單的,

從文檔上可以找到 QWidget 上有以下兩個與右鍵菜單有關的函數:

Qt.ContextMenuPolicy 是一個枚舉類型:

ConstantValueDescription
Qt.NoContextMenu0the widget does not feature a context menu, context menu handling is deferred to the widget's parent.
Qt.PReventContextMenu4the 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.DefaultContextMenu1the widget's QWidget.contextMenuEvent() handler is called.
Qt.ActionsContextMenu2the widget displays its QWidget.actions() as context menu.
Qt.CustomContextMenu3the widget emits the QWidget.customContextMenuRequested() signal.

現在主要要說的是利用 Qt.CustomContextMenu 來創建右鍵菜單.

QWidget 和它的子類 contextMenuPolicy 的默認值是 Qt.DefaultContextMenu 的,

所以我們需要通過 setContextMenuPolicy(QtCore.Qt.CustomContextMenu) 重新來設置他的值

(在Qt設計師中, 可以直接修改 contextMenuPolicy 的值為 CustomContextMenu 并且寫入到UI文件中,

所以用Qt設計師可以不用 setContextMenuPolicy 方法來設置)

 

CustomContextMenu 它所發出的是一個 customContextMenuRequested 信號 (signal) 如下:

這個信號是QWidget唯一與右鍵菜單有關的信號(也是自有的唯一信號), 同時也是很容易被忽略的信號(signal) 

*注: 文檔中QWidget方法和屬性巨量多, 以致我都看不到底部居然還有"一個"信號

既然有信號, 那么我們就可以輕松自定義我們想要的右鍵菜單了.

了解到這些之后, 我們就著手編寫槽(slot)了.

def myListWidgetContext(self, point):    popMenu = QtGui.QMenu()    popMenu.addAction(QtGui.QAction(u'添加', self))    popMenu.addAction(QtGui.QAction(u'刪除', self))    popMenu.addAction(QtGui.QAction(u'修改', self))    popMenu.exec_(QtGui.QCursor.pos())

接著就是連接槽

self.myListWidget.customContextMenuRequested[QtCore.QPoint].connect(self.myListWidgetContext)

文檔在手, 天下我有啊~

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海口市| 古交市| 绍兴市| 武强县| 武山县| 道孚县| 庄河市| 枞阳县| 康马县| 崇义县| 新巴尔虎左旗| 宁明县| 辽宁省| 耒阳市| 军事| 龙口市| 洪湖市| 余庆县| 年辖:市辖区| 英山县| 丹棱县| 永川市| 龙山县| 河东区| 恩施市| 遵化市| 泰安市| 孟村| 中方县| 台江县| 中宁县| 大城县| 郴州市| 时尚| 新河县| 富川| 常宁市| 永德县| 巢湖市| 洪洞县| 尖扎县|