PySide:QPushButton按下后保持突出显示 [英] PySide: QPushButton stays highlighted after pressing it

查看:158
本文介绍了PySide:QPushButton按下后保持突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工具中,当用户按下按钮时,将创建一个弹出窗口.我的问题是用户按下以调出窗口的按钮在弹出窗口创建时保持突出显示状态(好像我将鼠标放在其上),并且即使删除了弹出窗口也保持这种方式.我实际上喜欢弹出窗口处于活动状态时的突出显示(它在视觉上将窗口连接到弹出窗口,这很不错),但是我希望在删除窗口时消失.

In my tool, when the user pushes a button a popup window is created. My issue is that the button that the user presses to bring up the window stays highlighted (as if I have my mouse over it) on popup creation and remains that way even after the popup is deleted. I actually like this highlight while the popup is active (it visually connects the window to the popup which is nice), but I'd like it to go away when the window is deleted.

下面是一个示例,以阐明正在发生的事情:

Below is an example to clarify what's happening:

如果我单击创建资产,然后单击未成年人,则创建资产"按钮将保持突出显示状态

If I click on create asset, then click on minor save the create asset button stays highlighted

代码:

from PySide import QtCore, QtGui
import maya.OpenMayaUI as mui
from shiboken import wrapInstance 

def get_parent():
    ptr = mui.MQtUtil.mainWindow()
    return wrapInstance( long( ptr ), QtGui.QWidget )

############################################
class Tool_Window(QtGui.QDialog):
    def __init__(self, parent = get_parent() ):
        super(Tool_Window, self).__init__(parent)

        # Commands
        self.create_gui()
        self.create_layout()
        self.create_connections()

    #-------------------------------------------
    def create_gui(self):
        self.button1 = Push_Buttons()
        self.button1.setMaximumWidth(50)
        self.button2 = Push_Buttons()
        self.button2.setMaximumWidth(50)

    #-------------------------------------------
    def create_layout(self):
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        blank_layout = QtGui.QVBoxLayout()
        main_layout = QtGui.QHBoxLayout( self )
        main_layout.addLayout(blank_layout)
        main_layout.addLayout(layout)
        self.setLayout(layout)

    #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
    def create_connections(self):
        # Left click
        self.button1.clicked.connect( self.on_left_click )
        self.button2.clicked.connect( self.on_left_click )

    #-----#-----#-----#-----#-----#-----#-----#-----#-----#    
    def on_left_click(self):

        button = self.sender()

        self.popup = Popup_Window( self, button )                   
        self.popup.show()

############################################
class Push_Buttons( QtGui.QPushButton ):
    def __init__( self ):
        super( Push_Buttons, self ).__init__()

        self.setFocusPolicy(QtCore.Qt.NoFocus)

############################################
class Popup_Window( QtGui.QWidget ):
    def __init__( self, parent, button ):
        super( Popup_Window, self ).__init__(parent)

        self.setWindowFlags(QtCore.Qt.Popup)

        self.button_pos = button       
        self.parent = parent  

        self.setAttribute( QtCore.Qt.WA_DeleteOnClose )
        self.resize(230, 100)

        self.installEventFilter(self)

        self.create_gui()
        self.create_layout()
        self.create_connections()
        self.move_UI()   
        self.line_edit.setFocus()     

    #-------------------------------------------
    def create_gui( self ):
        ''' Visible GUI stuff '''
        self.my_label = QtGui.QLabel("default text")
        self.line_edit = QtGui.QLineEdit()
        self.line_edit.setMaxLength( 30 )
        self.push_btn = QtGui.QPushButton( "push" )
        self.push_btn.setMaximumWidth( 30 )

    #-------------------------------------------
    def create_layout( self ):

        self.button_layout = QtGui.QVBoxLayout()

        self.button_layout.addWidget( self.my_label, 0, 0 )
        self.button_layout.addWidget( self.line_edit, 1, 0 )
        self.button_layout.addWidget( self.push_btn, 2, 0 )

        self.setLayout(self.button_layout)

    #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
    def create_connections( self ):

        self.line_edit.textChanged.connect( self.on_text_changed )

    #-----#-----#-----#-----#-----#-----#-----#-----#-----#
    def on_text_changed( self, text ): 

        #---- set the text in label ----
        typed_name = self.line_edit.text()
        if " " in typed_name:
            typed_name.replace(" ", "")
        self.my_label.setText(typed_name) 

    #-------------------------------------------  
    def  eventFilter(self, source, event):

        if event.type() == QtCore.QEvent.WindowDeactivate:
            self.close()
        return QtGui.QWidget.eventFilter(self, source, event)

    #-------------------------------------------
    def move_UI( self ):
        self.line_edit.setFocus()
        y_btn = self.button_pos.mapToGlobal(QtCore.QPoint(0,0)).y()  
        x_win = self.parent.mapToGlobal(QtCore.QPoint(0,0)).x()

        w_pop = self.frameGeometry().width()

        x = x_win - w_pop - 12
        y = y_btn

        self.move(QtCore.QPoint(x,y))

############################################
if __name__ == '__main__':
    # Things to fix PySide Maya bug
    try:
        test_ui.close()
        test_ui.deleteLater()
    except:
        pass

    test_ui = Tool_Window()
    test_ui.show()

    try:
        test_ui.show()
    except:
        test_ui.close()
        test_ui.deleteLater()

推荐答案

在Windows 7和Ubuntu(QtCore.Qt.FocusPolicy.StrongFocus)上都将焦点策略设置为默认值时,我无法重现您的问题.但是,在将按钮的焦点策略设置为QtCore.Qt.FocusPolicy.NoFocus之后,在两个系统上都存在.

I was not able to reproduce your issue when the focus policy was set to the default value on both Windows 7 and Ubuntu (QtCore.Qt.FocusPolicy.StrongFocus). However, it was on both system after I've set the focus policy of the buttons to QtCore.Qt.FocusPolicy.NoFocus.

为解决此问题,我建议暂时在注册了close事件时从Popup_WindoweventFilter方法强制重新绘制Tool_Window实例,如下所示:

To solve this issue, I suggest, for the moment, to force a repaint of the Tool_Window instance, from the eventFilter method of the Popup_Window, when a close event is registered, as shown below:

def  eventFilter(self, source, event):

    if event.type() == QtCore.QEvent.WindowDeactivate:
        self.close()
    elif event.type() == QtCore.QEvent.Close:
        self.parent.repaint()
    return QtGui.QWidget.eventFilter(self, source, event)

当按钮的焦点策略设置为QtCore.Qt.FocusPolicy.NoFocus时,它已经为我解决了Windows7和Ubuntu上的问题.我可能会进行进一步调查以更好地了解发生了什么,我会及时通知您.

It has solved the problem on both Windows7 and Ubuntu for me when the focus policity of the button was set to QtCore.Qt.FocusPolicy.NoFocus. I may investigate further to better understand what is going on, I'll keep you posted.

旁注::我没有使用OpenMayaUI测试您的代码,所以也许这就是为什么默认情况下我没有得到此问题的原因,而只是在我明确设置了按钮的焦点策略之后到NoFocus.也许OpenMayaUI强制默认情况下按钮具有NoFocus策略.也可能是因为我们的操作系统和主题之间存在差异.

side note : I am not testing your code with OpenMayaUI, so maybe that is why I do not get the issue by default, but only after I explicitly set the focus policy of the buttons to NoFocus. Maybe OpenMayaUI force your buttons to have a NoFocus policy by default. It can be also because of differences between our OS and theme.

这篇关于PySide:QPushButton按下后保持突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆