更改QMenu中的指定行

2024-04-25 06:15:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的脚本,就像我上面添加的。我的问题是改变一个指定的行在弹出菜单没有添加所有一次又一次,因为它太慢,刷新所有弹出行,导致闪烁。你知道吗

我的代码是:

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_())

提前谢谢! 克


Tags: posselfeventifinitdeflineaction
3条回答

不完全确定要做什么,但代码中存在各种问题。例如,您正在使用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_())

我让你让他们一起工作-这应该很容易。你知道吗

相关问题 更多 >