在QMenu中更改指定的行

时间:2015-09-12 23:47:49

标签: python pyqt pyside

我有一个像上面添加的简单脚本。我的问题是更改弹出菜单中的指定行而不是一次又一次地添加所有行,因为它太慢并刷新导致闪烁的所有弹出行。

我的代码是:

import sys
from PySide import QtGui
from PySide import QtCore

class MyLabel(QtGui.QLabel):
    def __init__(self,action, line_number = 0, sel_line = -1):
        super(MyLabel,self).__init__()
        self.action = action
        self.SelectedStyle   = "QLabel { color: rgb( 22, 22, 250 ); font-weight:normal ; font-size: 11px; background-color: rgb(1, 1, 238, 255); border: 1px solid rgba(88, 88, 88, 10); }" 
        # 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.NormalStyle     = "QLabel { color: rgb( 22, 22, 22 ); font-weight:normal ; font-size: 11px; background-color: rgb(238, 238, 238, 255); border: 1px solid rgba(88, 88, 88, 10); }" 

        if line_number == sel_line:# and self.parent().in_focus == False:
            self.NormalStyle = self.SelectedStyle
            self.setStyleSheet( self.NormalStyle)

    # 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):
        self.setStyleSheet( self.NormalStyle)

class Example(QtGui.QMainWindow):

    def __init__(self):
        super(Example, self).__init__()

        mpos = QtGui.QCursor
        x = mpos.pos().x()
        y = mpos.pos().y()
        self.qmenu = popQMenu( )

        self.qmenu.show()
        self.qmenu.setGeometry( x-11, y-11, 101, 101)
        # self.show()
        self.qmenu.exec_()



        qMenu.setGeometry( x-10, y-10, 0, 0)
        qMenu.show()
        qMenu.exec_()

        # self.setWindowTitle('Menubar')    
        # self.show()

class popQMenu( QtGui.QMenu):
    def __init__( self):

        super( popQMenu, self).__init__()
        self.sel_line_pos = 2
        self.ShowPopup()

    def ShowPopup(self):
        self.clear()
        for i in range( 52):
            wAction = QtGui.QWidgetAction(self)
            ql = MyLabel(wAction, i, self.sel_line_pos )
            ql.setText("<u>Hello</u> <i>Qt!</i>")
            wAction.setData( '1')
            wAction.setDefaultWidget(ql)
            self.addAction(wAction)

    def MenuSelected( self):
        action = self.sender()
        print action.data()

    def mousePressEvent(self, event):
        if event.buttons() == QtCore.Qt.RightButton:
            print 'right mouse pressed'
            pass
    # check for the typed string for filtering it
    def keyPressEvent( self, event):
        if event.key() == QtCore.Qt.Key_Up:
            print self.sel_line_pos
            if self.sel_line_pos >= 1:
                self.sel_line_pos -= 1

        elif event.key() == QtCore.Qt.Key_Down:
            print self.sel_line_pos
            if self.sel_line_pos < 22:
                self.sel_line_pos += 1
        self.ShowPopup()



app = QtGui.QApplication(sys.argv)
ex = Example()

sys.exit(app.exec_())

提前致谢! ģ

1 个答案:

答案 0 :(得分:0)

不完全确定您要执行的操作,但代码中存在各种问题。例如,您使用的是background-color而不是background

另一个问题是,MyLabel首次创建时NormalStyle未设置为__init__

通过这两个更改,鼠标光标部分似乎主要适合我。另一方面,您现在似乎遇到了光标键部分的问题。以下是如何实现这一目标的简要示例:

import sys
from PySide import QtGui
from PySide import QtCore

SelectedStyle   = "QLabel { color: rgb( 22, 22, 250 ); font-weight:normal ; font-size: 11px; background-color: rgb(1, 1, 238, 255); border: 1px solid rgba(88, 88, 88, 10); }" 
NormalStyle     = "QLabel { color: rgb( 22, 22, 22 ); font-weight:normal ; font-size: 11px; background-color: rgb(238, 238, 238, 255); border: 1px solid rgba(88, 88, 88, 10); }" 

class MyLabel(QtGui.QLabel):
    def __init__(self,action, line_number = 0, sel_line = -1):
        super(MyLabel,self).__init__()
        self.action = action

class Example(QtGui.QMainWindow):

    def __init__(self):
        super(Example, self).__init__()

        mpos = QtGui.QCursor
        x = mpos.pos().x()
        y = mpos.pos().y()
        self.qmenu = popQMenu( )
        self.qmenu.show()
        self.qmenu.setGeometry( x-11, y-11, 101, 101)
        self.qmenu.exec_()

class popQMenu( QtGui.QMenu):
    def __init__( self):
        super( popQMenu, self).__init__()
        self.sel_line_pos = 2
        self.widgets = []
        for i in range( 22):
            wAction = QtGui.QWidgetAction(self)
            ql = MyLabel(wAction, i, self.sel_line_pos )
            ql.setStyleSheet( NormalStyle)
            ql.setText("<u>Hello</u> <i>Qt!</i>")
            self.widgets.append(ql)
            wAction.setDefaultWidget(ql)
            self.addAction(wAction)
        self.widgets[self.sel_line_pos].setStyleSheet( SelectedStyle)

    def keyPressEvent( self, event):
        if event.key() == QtCore.Qt.Key_Up:
            if self.sel_line_pos >= 1:
                self.widgets[self.sel_line_pos].setStyleSheet( NormalStyle)
                self.sel_line_pos -= 1
                self.widgets[self.sel_line_pos].setStyleSheet( SelectedStyle)

        elif event.key() == QtCore.Qt.Key_Down:
            if self.sel_line_pos < 22:
                self.widgets[self.sel_line_pos].setStyleSheet( NormalStyle)
                self.sel_line_pos += 1
                self.widgets[self.sel_line_pos].setStyleSheet( SelectedStyle)


app = QtGui.QApplication(sys.argv)
ex = Example()

sys.exit(app.exec_())

我会留给你让他们一起工作 - 这应该很容易。