欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pyside+pyqt實(shí)現(xiàn)鼠標(biāo)右鍵菜單功能

 更新時(shí)間:2020年12月08日 15:09:27   作者:Time said  
這篇文章主要為大家詳細(xì)介紹了pyside+pyqt實(shí)現(xiàn)鼠標(biāo)右鍵菜單功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了pyside pyqt實(shí)現(xiàn)鼠標(biāo)右鍵菜單功能的具體代碼,供大家參考,具體內(nèi)容如下

在三維軟件中使用pyside/pyqt編寫gui界面時(shí),為了藝術(shù)家使用操作的簡(jiǎn)潔,以及方便,經(jīng)常會(huì)使用鼠標(biāo)右鍵菜單進(jìn)行界面與功能的交互。下面就介紹一下這一功能,當(dāng)然了網(wǎng)上也有很多案列可供參考。

# -*- encoding: utf-8 -*-
try:
 from PySide import QtGui 
 from PySide import QtCore
except ImportError:
 from PySide2 import QtWidgets as QtGui
 from PySide2 import QtCore
import sys
class MainWindow(QtGui.QMainWindow):
 def __init__(self):
 super(MainWindow, self).__init__()
 self.createContextMenu()

 def createContextMenu(self):
 ''''' 
 創(chuàng)建右鍵菜單 
 '''
 # 必須將ContextMenuPolicy設(shè)置為Qt.CustomContextMenu
 # 否則無法使用customContextMenuRequested信號(hào)
 self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
 self.customContextMenuRequested.connect(self.showContextMenu)

 # 創(chuàng)建QMenu
 self.contextMenu = QtGui.QMenu(self)
 self.actionA = self.contextMenu.addAction(u'添加')
 self.actionB = self.contextMenu.addAction(u'刪除')
 # 將動(dòng)作與處理函數(shù)相關(guān)聯(lián)
 # 這里為了簡(jiǎn)單,將所有action與同一個(gè)處理函數(shù)相關(guān)聯(lián),
 # 當(dāng)然也可以將他們分別與不同函數(shù)關(guān)聯(lián),實(shí)現(xiàn)不同的功能
 self.actionA.triggered.connect(self.actionHandler)
 self.actionB.triggered.connect(self.actionHandler)

 def showContextMenu(self, pos):
 ''''' 
 右鍵點(diǎn)擊時(shí)調(diào)用的函數(shù) 
 '''
 # 菜單顯示前,將它移動(dòng)到鼠標(biāo)點(diǎn)擊的位置
 self.contextMenu.move(QtGui.QCursor().pos())
 self.contextMenu.show()

 def actionHandler(self):
 ''''' 
 菜單中的具體action調(diào)用的函數(shù) 
 '''
 print 'action handler'

if __name__ == '__main__':
 app = QtGui.QApplication(sys.argv)
 window = MainWindow()
 window.show()
 sys.exit(app.exec_())

簡(jiǎn)單的右鍵菜單就實(shí)現(xiàn)了,連接功能就學(xué)要按照需求進(jìn)行添加。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論