Python 动态QMenu列表
首先,我是python pyside的新手。谢谢你的耐心。我启动一个脚本的原因是从使用QMenu的列表中打开文件。但我想在显示列表时将其扩展为一个键入功能,并且键入的字母将过滤QMenu列表。我需要一些帮助。请。 我发现了一个类似的主题,但我并不理解它是如何工作的,以及如何实现它。 我的部分代码是: 在这里输入代码Python 动态QMenu列表,python,qt,pyside,Python,Qt,Pyside,首先,我是python pyside的新手。谢谢你的耐心。我启动一个脚本的原因是从使用QMenu的列表中打开文件。但我想在显示列表时将其扩展为一个键入功能,并且键入的字母将过滤QMenu列表。我需要一些帮助。请。 我发现了一个类似的主题,但我并不理解它是如何工作的,以及如何实现它。 我的部分代码是: 在这里输入代码 # class of each line of the list class MyLabel(QtGui.QLabel): def __init__(self,action,
# class of each line of the list
class MyLabel(QtGui.QLabel):
def __init__(self,action, mnu_type, id):
# maybe action input not needed
super(MyLabel,self).__init__()
rep_col = [0,-4]
bgcol_val = [238+rep_col[id%2], 238+rep_col[id%2], 238+rep_col[id%2], 255]
bgcolsel_val = [72, 118, 188, 255]
bgcolnc_val = [145+rep_col[id%2], 215+rep_col[id%2], 225+rep_col[id%2], 75]
txtcol = 'rgb( 50, 50, 50 )'
seltxtcol = 'rgb( 250, 250, 250 )'
bgcol = 'rgba(%s,%s,%s,%s)'%(bgcol_val[0],bgcol_val[1],bgcol_val[2],bgcol_val[3])
bgcolsel = 'rgba(%s,%s,%s,%s)'%(bgcolsel_val[0],bgcolsel_val[1],bgcolsel_val[2],bgcolsel_val[3])
bgcolnc = 'rgba(%s,%s,%s,%s)'%(bgcolnc_val[0],bgcolnc_val[1],bgcolnc_val[2],bgcolnc_val[3])
border = 'rgba(88, 88, 88, 10)'
fontsize = 11
# style of selected line
self.SelectedStyle = "QLabel { color: %s; font-weight:normal ; font-size: %dpx; background-color: %s; border: 1px solid %s; }" % ( seltxtcol,fontsize,bgcolsel,border)
# style of the non commercial types
if mnu_type == '.hipnc':
self.NormalStyle = "QLabel { color: %s ; font-weight:normal ; font-size: %dpx; background-color: %s; border: 1px solid %s; } " % ( txtcol,fontsize,bgcolnc,border )
# style of main tittle
elif mnu_type == 'tittle':
self.NormalStyle = "QLabel { color: %s; font-weight:bold ; font-size: %dpx; background-color: rgba(125, 125, 115, 255); border: 3px solid %s; } " % ( txtcol,fontsize,border )
self.SelectedStyle = self.NormalStyle
# style of empty line ( last )
elif mnu_type == 'empty':
self.NormalStyle = "QLabel { color: %s; font-weight:normal ; font-size: 7px; background-color: %s; } " % ( txtcol,bgcol)
self.SelectedStyle = self.NormalStyle
# style of all non selected lines
else:
self.NormalStyle = "QLabel { color: %s; font-weight:normal ; font-size: %dpx; background-color: %s; border: 1px solid %s; } " % ( txtcol,fontsize,bgcol,border )
self.setStyleSheet( self.NormalStyle)
# self.action = action
# def mouseReleaseEvent(self,e):
# self.action.trigger( )
# colorize the selected line
def enterEvent( self, event):
self.setStyleSheet( self.SelectedStyle)
# set back the not selection line to default color
def leaveEvent( self, event):
print
self.setStyleSheet( self.NormalStyle)
# the menu itself you must create an instace
# from it to add events to it
class MyQMenu( QtGui.QMenu):
"""docstring for MyQmenu"""
def __init__(self):
super(MyQMenu, self).__init__()
self.search_string = ''
# check for the typed string for filtering it
def keyPressEvent( self, event):
if event.key() == QtCore.Qt.Key_Plus:
pass
elif event.key() == QtCore.Qt.Key_Backspace:
self.search_string = self.search_string[:-1]
elif event.key() == QtCore.Qt.Key_Escape:
if self.search_string == '':
self.close()
self.search_string = ''
elif event.key() in range(256):
self.search_string += ( chr(event.key()))
print self.search_string
# the main window
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
mpos = QtGui.QCursor
x = mpos.pos().x()
y = mpos.pos().y()
acts = []
# self.qmenu = QtGui.QMenu()
self.qmenu = MyQMenu()
self.hip_fgrp = HipFileGroup( hip_data_file )
self.hip_fgrp.RemoveRepeats()
titleAction = self.myAction( "tittle", "hipBrowser v0.1beta", -1)
acts.append( titleAction)
for i,hipf in enumerate(self.hip_fgrp.hipFileArr):
short_n = hipf.shortname
prj = ' ' + hipf.shortprjname
if len(hipf.add_hipfolders):
prj = prj + ' \\ ' + hipf.add_hipfolders[0]
wAction = self.myAction( hipf.extension, prj+' \\ '+short_n, i)
acts.append( wAction)
titleAction = self.myAction( "empty", "", -1)
acts.append( titleAction)
for action in acts:
self.qmenu.addAction(action)
self.qmenu.show()
self.qmenu.setGeometry( x-20, y-20, 0, 0)
# self.show()
self.qmenu.exec_()
# action of one line: type = separate header etc,
# name = menu line string, data
# data = identifies the file in array
def myAction( self, atype, name, id):
wAction = QtGui.QWidgetAction(self)
ql = MyLabel(wAction, atype, id)
# ql.setText("<b>Hello</b> <i>Qt!</i>")
ql.setText( name)
wAction.setDefaultWidget(ql)
wAction.triggered.connect( self.MenuSelected)
wAction.setData( id)
return wAction
def MenuSelected( self):
action = self.sender()
# if the line is header or empty line
if action.data() == -1:
return
hipfile_id = action.data()
hipfile = self.hip_fgrp.hipFileArr[ hipfile_id]
hipfile.show_all()
hipfile_last = hipfile.getLastVersion( hipfile.hipfullspec)
self.qmenu.close()
if not in_sublime:
import hou
hou.hipFile.load( hipfile_last, hip_accept)
#列表每行的类
MyLabel类(QtGui.QLabel):
定义初始化(自我、动作、mnu类型、id):
#也许不需要行动输入
超级(MyLabel,self)。\uuuu init
rep_col=[0,-4]
bgcol_val=[238+rep_col[id%2],238+rep_col[id%2],238+rep_col[id%2],255]
bgcolsel_val=[72118188255]
bgcolnc_val=[145+rep_col[id%2],215+rep_col[id%2],225+rep_col[id%2],75]
txtcol='rgb(50,50,50)'
seltxtcol='rgb(250250250)'
bgcol='rgba(%s,%s,%s,%s)'(bgcol_val[0],bgcol_val[1],bgcol_val[2],bgcol_val[3])
bgcolsel='rgba(%s,%s,%s,%s)'(bgcolsel_val[0],bgcolsel_val[1],bgcolsel_val[2],bgcolsel_val[3])
bgcolnc='rgba(%s,%s,%s,%s)'(bgcolnc_val[0],bgcolnc_val[1],bgcolnc_val[2],bgcolnc_val[3])
边框='rgba(88,88,88,10)'
fontsize=11
#所选行的样式
self.SelectedStyle=“QLabel{color:%s;字体大小:正常;字体大小:%dpx;背景色:%s;边框:1px实心%s;}”%(seltxtcol,fontsize,bgcolsel,border)
#非商业类型的风格
如果mnu_type='.hipnc':
self.NormalStyle=“QLabel{color:%s;字体重量:正常;字体大小:%dpx;背景色:%s;边框:1px实心%s;}”%(txtcol,fontsize,bgcolnc,border)
#标题风格
elif mnu_类型=='title':
self.NormalStyle=“QLabel{color:%s;字体重量:粗体;字体大小:%dpx;背景色:rgba(125125115255);边框:3px实心%s;}”%(txtcol,fontsize,边框)
self.SelectedStyle=self.NormalStyle
#空行样式(最后一个)
elif mnu_类型=='empty':
self.NormalStyle=“QLabel{color:%s;字体大小:normal;字体大小:7px;背景色:%s;}”%(txtcol,bgcol)
self.SelectedStyle=self.NormalStyle
#所有非选定行的样式
其他:
self.NormalStyle=“QLabel{color:%s;字体重量:正常;字体大小:%dpx;背景色:%s;边框:1px实心%s;}”%(txtcol,fontsize,bgcol,border)
self.setStyleSheet(self.NormalStyle)
#行动
#def mouseReleaseEvent(自身,e):
#self.action.trigger()
#为所选行着色
def enterEvent(自我,事件):
self.setStyleSheet(self.SelectedStyle)
#将非选择行设置回默认颜色
事件(自身、事件):
打印
self.setStyleSheet(self.NormalStyle)
#菜单本身必须创建Instance
#从它添加事件到它
类MyQMenu(QtGui.QMenu):
“”“MyQmenu的docstring”“”
定义初始化(自):
超级(MyQMenu,self)。\uu初始化
self.search_字符串=“”
#检查键入的字符串以进行筛选
def按键事件(自身,事件):
如果event.key()==QtCore.Qt.key\u Plus:
通过
elif event.key()==QtCore.Qt.key\u退格:
self.search\u string=self.search\u string[:-1]
elif event.key()==QtCore.Qt.key\u转义:
如果self.search_字符串=='':
self.close()
self.search_字符串=“”
范围(256)中的elif event.key():
self.search_string+=(chr(event.key()))
打印self.search\u字符串
#主窗口
类主窗口(QtGui.QMainWindow):
定义初始化(自):
超级(主窗口,自我)。\uuuu初始化
mpos=QtGui.QCursor
x=mpos.pos().x()
y=mpos.pos().y()
acts=[]
#self.qmenu=QtGui.qmenu()
self.qmenu=MyQMenu()
self.hip\u fgrp=HipFileGroup(hip\u数据\u文件)
self.hip_fgrp.removeepeats()
titleAction=self.myAction(“title”,“hipBrowser v0.1beta”,-1)
法案附加(滴定法)
对于i,枚举中的hipf(self.hip_fgrp.hipFileArr):
short\u n=hipf.shortname
prj=''+hipf.shortprjname
如果len(hipf.add\u hipfolders):
prj=prj+'\\'+hipf。添加\u hipf文件夹[0]
waaction=self.myAction(hipf.extension,prj+'\\\'+short\n,i)
附加法案(WAAction)
titleAction=self.myAction(“空”、“空”、“1”)
法案附加(滴定法)
在法案中采取行动:
self.qmen.addAction(操作)
self.qmenu.show()
self.qmen.setGeometry(x-20、y-20、0、0)
#self.show()
self.qmenu.exec_389;()
#一行的作用:类型=单独的标题等,
#名称=菜单行字符串,数据
#data=标识数组中的文件
def myAction(self、atype、name、id):
wAction=QtGui.QWidgetAction(self)
ql=MyLabel(wAction,atype,id)
#ql.setText(“Hello Qt!”)
ql.setText(名称)
setDefaultWidget(ql)
waaction.triggered.connect(self.MenuSelected)
wAction.setData(id)
回程
已选择def菜单(自):
action=self.sender()
#如果行是标题行还是空行
如果action.data()==-1:
回来
hipfile_id=action.data()
hipfile=self.hip\u fgrp.hipFileArr[hipfile\u id]
hipfile.show_all()
hipfile\u last=hipfile.getLastVersion(hipfile.hipfullspec)
self.qmen.close()
如果不是崇高的:
进口侯
hou.hipFile.load(hipFile\u last,hip\u accept)
我是否理解正确:您有一个QMenu
,其中每个操作都以一个文件命名(从列表中获取),单击打开相应的文件?您是对的。重点是通过实际键入的文本在allways刷新菜单上。我需要一些想法,例如