动态QMenu列表

时间:2015-07-09 21:42:46

标签: python qt pyside

首先,我是python pyside的新手。所以感谢耐心。我启动了一个脚本,原因是从使用QMenu的列表中打开文件。但是我希望在列表显示和键入的字母过滤QMenu列表时将其扩展为键入功能。我需要一些帮助。请。 我发现了一个类似的主题,但我只是不知道它是如何工作的,我怎么能实现它。 It is here. 我的部分代码是:     在这里输入代码

# 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)

0 个答案:

没有答案