子类化 QLabel 以显示本机“鼠标悬停按钮指示器”
我有一个带有“StyledPanel,凸起”框架的 QLabel。
通过子类化 QLabel,它是可点击的;
class InteractiveLabel(QtGui.QLabel):
def __init__(self, parent):
QtGui.QLabel.__init__(self, parent)
def mouseReleaseEvent(self, event):
self.emit(QtCore.SIGNAL('clicked()'))
然而,普遍的观点是这个“框”不容易被识别为可点击。
为了提高可用性,我希望“框”显示当鼠标悬停在其上时可以单击它。
显然,通过连接 mouseHoverEvent 可以轻松实现对鼠标悬停的反应。
但是,“按钮指示器”必须是本机继承的,因为我的 Qt 应用程序允许用户更改样式(在 Windows XP、Windows 7、plastique、motif、cde 之外)。
该图像以两种不同的风格显示了我想要的特定小部件(右下角)和鼠标悬停美学。
当鼠标悬停在“Box”上时,我希望它能够像组合框在顶部、中间那样做出响应。
(“响应”在美学上是原生的,并且出现在所有 Qt 按钮中,除了“CDE”和“motif”样式。)。
有没有办法用 PyQt4 来实现这个?
(我认为非本机解决方案将涉及 QGradient 并检查本机样式,但这很糟糕。)
更新:
lol4t0 的 QLabel 和 QPushButton 的想法。
这是我的 Python 实现,信号正常工作,并且具有所有适当的按钮美观。 RichTextBox 是您要嵌入到程序中的小部件。
from PyQt4 import QtCore, QtGui
class RichTextButton(QtGui.QPushButton):
def __init__(self, parent=None):
QtGui.QPushButton.__init__(self, parent)
self.UnitText = QtGui.QLabel(self)
self.UnitText.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.UnitText.setAlignment(QtCore.Qt.AlignCenter)
self.UnitText.setMouseTracking(False)
self.setLayout(QtGui.QVBoxLayout())
self.layout().setMargin(0)
self.layout().addWidget(self.UnitText)
谢谢!
规格:
- 蟒蛇2.7.2
- Windows 7
-PyQt4
I have a QLabel with a 'StyledPanel, raised' frame.
It is clickable, by subclassing QLabel;
class InteractiveLabel(QtGui.QLabel):
def __init__(self, parent):
QtGui.QLabel.__init__(self, parent)
def mouseReleaseEvent(self, event):
self.emit(QtCore.SIGNAL('clicked()'))
However, a general opinion is that this 'Box' is not easily recognised as clickable.
In an effort toward usability, I'd like the 'Box' to show it is clickable when the mouse is hovered over it.
Obviously a reaction to a mouse hover is easily achieved by connecting the mouseHoverEvent.
However, the 'button indicator' must be natively inherited, since my Qt application allows the User to change the style (out of Windows XP, Windows 7, plastique, motif, cde).
This image shows the particular widget (bottom right corner) and the mouseHover aesthetics I desire in two different styles.
When a mouse is hovered over 'Box', I'd like it to respond like the combobox has in the top, middle.
(The 'response' is aesthetically native and occurs with all Qt buttons, except in 'CDE' and 'motif'styles.).
Is there a way to implement this with PyQt4?
(I suppose non-native solutions would involve QGradient and checking the native style, but that's yucky.)
UPDATE:
lol4t0's idea of a QLabel over a QPushButton.
Here's my pythonic implementation with signals working properly and all the appropriate button aesthetics.
The RichTextBox is the widget you would embed into the program.
from PyQt4 import QtCore, QtGui
class RichTextButton(QtGui.QPushButton):
def __init__(self, parent=None):
QtGui.QPushButton.__init__(self, parent)
self.UnitText = QtGui.QLabel(self)
self.UnitText.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.UnitText.setAlignment(QtCore.Qt.AlignCenter)
self.UnitText.setMouseTracking(False)
self.setLayout(QtGui.QVBoxLayout())
self.layout().setMargin(0)
self.layout().addWidget(self.UnitText)
Thanks!
Specs:
- python 2.7.2
- Windows 7
- PyQt4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要思想
您可以在
QPushButton
上方添加QLabel
(使QLabel
成为QPushButton
的子级)并在标签中显示富文本,而点击和装饰可以用QPushButton
来处理实验
好吧,我是一个
C++
程序员,但没有什么复杂的,我希望,你理解代码实现主要思想:
这几乎有效!唯一的一个愚蠢的错误(或我的全局误解)是,当您用鼠标按下按钮然后释放它时,它保持按下状态。
- 所以,看来我们需要在label
中重新实现mouseReleaseEvent
来修复总是按下问题:很确定,有一个更优雅的解决方案,但我现在懒得找到它,所以,我做了以下操作:
正如 @Roku 所说,要解决这个问题,我们必须添加
Main idea
You can add
QLabel
aboveQPushButton
(makeQLabel
child ofQPushButton
) and show rich text in label, while clicks and decorations can be processed withQPushButton
Experiment
Well, I am a
C++
programmer, but there is nothing complicated, I hope, you understand the codeImplementing main idea:
And this almost works! The only one silly bug (or my global misunderstanding) is that when you press button with mouse and then release it, it remains pressed.
- So, it seems we need to reimplementmouseReleaseEvent
in ourlabel
to fix always pressed issue:I'm pretty sure, there is a bit more elegant solution, but I'm too lazy to find it now, so, I made following:
As @Roku said, to fix that issue, we have to add
@Lol4t0,我对你的方法做了一些改进...
这是我的头文件:
还有我的cpp文件:
所以QLabel上的文本始终可见。 QPushButton 不能太小而无法隐藏部分文本。我觉得这种方式用起来比较舒服...
@Lol4t0, i have some improvements for your method...
This is my header file:
And there is my cpp file:
So text on QLabel is always visible. QPushButton can't be too small to hide part of text. I think this way is more comfortable to use...