如何在 PyQt 中为 Qt-Designer 小部件实现 MousePressEvent
我有一个小部件(QTabeleWidget、QLabels 和一些 QButton)。它是用 Qt-Designer 构建的,现在我必须实现一些东西。为此我需要 mousePressEvent。 通常我会编写一个子类并编写如下内容:
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
print "left"
else:
print 'right'
但我不知道如何为设计器中创建的小部件执行此操作。 我需要它用于 QTableleWidget。希望有人能帮助我。我尝试在google的帮助下解决这个问题,但没有成功。 这个网站给了我很多帮助,所以我想我应该尝试一下并询问。
I've got a Widget (QTabeleWidget, QLabels and some QButtons). It was built in Qt-Designer, and now I have to implement some things. For that I need the mousePressEvent.
Usually I would write a subclass and write something like this:
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
print "left"
else:
print 'right'
But I don't know how to do that for a Widget created in the Designer.
I need it for the QTabeleWidget. Hope someone can help me. I tried to solve the problem with the help of google, but without success.
This site helped me many times, so I thought I'll give it a shot and ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 PyQt,可以通过三种不同的方式处理设计器中创建的表单:
单继承:
多重继承:
动态生成:
对于上面的(3),您最终将得到您为表单指定的任何基本类型的实例。然后,您可以根据需要重写
mousePressEvent
。我建议您查看 PyQt4 参考手册中的第 13.1 节 。 13.2 节讨论了 uic 模块。
With PyQt there are three different ways to work with forms created in designer:
Single Inheritance:
Multiple Inheritance:
Dynamically Generated:
For (3) above, you'll end up with an instance of whatever base type you specified for your form. You can then override your
mousePressEvent
as desired.I'd recommend you take a look at section 13.1 in the PyQt4 reference manual. Section 13.2 talks about the
uic
module.