- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的工具中,当用户按下按钮时,会创建一个弹出窗口。我的问题是用户按下以调出窗口的按钮在弹出窗口创建时保持突出显示(就像我将鼠标悬停在它上面一样)并且即使在删除弹出窗口后仍然保持这种状态。我实际上喜欢弹出窗口处于事件状态时的此突出显示(它在视觉上将窗口连接到弹出窗口,这很好),但我希望它在删除窗口时消失。
下面是一个例子来阐明正在发生的事情:
如果我点击创建 Assets ,然后点击次要保存创建 Assets 按钮保持突出显示
代码:
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
之后,它出现在两个系统上。
为了解决这个问题,目前我建议从 Popup_Window
eventFilter
方法强制重绘 Tool_Window
实例,注册关闭事件时,如下图:
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 上的问题。我可能会进一步调查以更好地了解发生了什么,我会及时通知您。
旁注:我没有使用 OpenMayaUI
测试您的代码,所以也许这就是默认情况下我没有遇到问题的原因,但只有在我明确设置按钮的焦点策略为 NoFocus
。也许 OpenMayaUI
会强制您的按钮在默认情况下具有 NoFocus
策略。这也可能是因为我们的操作系统和主题之间存在差异。
关于focus - PySide:QPushButton 在按下后保持突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31734925/
我想更新小部件的值,但为了防止无限循环,我需要防止调用此小部件的回调函数 .valueChanged信号。 以下示例完全按预期工作: Qt = PySide class MainWindow(Acto
使用以下代码,在尝试发出信号时出现错误(“PySide.QtCore.Signal”对象没有属性“emit”): #!/usr/bin/env python from PySide import Qt
我是 Python 新手。我同时安装了 Python 2.7 和 Python 3。我刚刚尝试通过 Homebrew 安装 PySide 并收到此消息: PySide package successf
是否可以使用 PySide 中的 Web 类之一加载包含按钮的本地 html/JS 文件,并将这些按钮连接到 PySide 插槽? 最佳答案 您可以使用 QtWebKit 将 QObject 导出到
我一直在寻找一个工作示例,该示例如何在使用 QT 设计器创建的 pyside 中嵌入 matplotlib 图,同时将逻辑保存在单独的文件中。我知道网上有很多例子,但没有一个真正使用 QT 设计器,然
我想我可以通过将\PySide 文件夹附加到我的环境的“搜索路径”来添加 PySide 库,但智能感知似乎没有发现任何东西。不过,它正在使用我的其他图书馆。有什么建议吗? 注意:我已经安装了 PTVS
我正在设计一个 Pyside Qt 应用程序,我想切换 QtCore.Qt.WindowStaysOnTopHint我的主窗口中的窗口标志。使用此代码设置此提示工作正常: self.setWindow
你如何在 PySide 中禁用声子? 我正在尝试使用 QWebpage 并且我不希望 phonon 启动,因为我正在编写的应用程序是服务器端应用程序,并且不需要媒体。我尝试了一些东西,但没有找到任何真
本文为大家分享了pyside pyqt实现鼠标右键菜单功能的具体代码,供大家参考,具体内容如下 在三维软件中使用pyside/pyqt编写gui界面时,为了艺术家使用操作的简洁,以及方便,经常会使
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我试图在 PySide 的单独线程中运行一个长任务,以便主线程可以继续处理 GUI 事件。我已经阅读了执行此操作的正确方法: 将任务封装在 QObject 子类中,其中工作在 run() 方法中完成,
我想知道如何根据最大标签的最大宽度/高度截断QLabel中的文本。 输入的文本可以是任意长度,但是为了保持整洁的布局,我想截断长字符串以填充最大空间(小部件的最大宽度/高度)。 例如。: 'A ve
我有一个界面,屏幕顶部有 4 个按钮,下面有 QFrame。我想根据按下的按钮更改 QFrame 中的布局。例如,如果按下 Button_1 显示一个 TextEdit Widget,如果按下 But
我想制作一个小部件的角。在 PyQt 中,我用样式表解决了这个问题,效果很好。我想在 PySide 中做同样的事情,但由于某些原因它不起作用。 这是我的代码: TitelWidget = QtGui.
我是 python 和 pyside 的新手。我将尝试成功运行以下代码。但现在我希望程序在显示窗口后等待用户无法使用它的定义时间,然后升级状态栏。我会尝试 sleep() 但不知道它必须以正确的方式放
我从 example of Zetcode 开始学习 PySide并尝试编写具有两个窗口的应用程序:“布局 View ”的父级“示意图 View ”,每个窗口都有菜单栏。启动时应该只是原理图窗口,布局
我从 example of Zetcode 开始学习 PySide并尝试编写具有两个窗口的应用程序:“布局 View ”的父级“示意图 View ”,每个窗口都有菜单栏。启动时应该只是原理图窗口,布局
我正在尝试通过 pyside 设置 QGridLayout 小部件的背景图像(位于与应用程序相同的文件夹中),但无济于事我已经查看了有关样式表的所有图、文档和论坛帖子,但仍然我不知道我做错了什么。 f
我有一个带有 5 个按钮的布局,我充当“菜单”,因此您单击一个按钮,会显示一个 View ,单击另一个按钮,会显示另一个 View 。我需要找出单击了哪个按钮,以便我可以根据按下的按钮执行某些操作。类
基本上我想要的是: 在主窗口中显示一个小部件,其中包含打开 QFileDialog 的按钮 选择文件后,包含按钮的小部件应切换到新的小部件,该新小部件会显示一些基于文件内容的可视化效果。 在下面的代码
我是一名优秀的程序员,十分优秀!