如何在 PyQt 中为 Qt-Designer 小部件实现 MousePressEvent

发布于 2024-09-15 14:22:17 字数 397 浏览 4 评论 0原文

我有一个小部件(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹淡然 2024-09-22 14:22:17

使用 PyQt,可以通过三种不同的方式处理设计器中创建的表单:

  1. 使用单继承并使表单成为成员变量
  2. 使用多重继承
  3. 直接从 UI 文件动态生成成员

单继承

class MyTableWidget(QTableWidget):
    def __init__(self, parent, *args):
        super(MyTableWidget, self).__init__(parent, args)
        self.ui = YourFormName()
        self.ui.setupUi(self)
        # all gui elements are now accessed through self.ui
    def mousePressEvent(self, event):
        pass # do something useful

多重继承

class MyTableWidget(QTableWidget, YourFormName):
    def __init__(self, parent, *args):
        super(MyTableWidget, self).__init__(parent, args)
        self.setupUi(self)
        # self now has all members you defined in the form
    def mousePressEvent(self, event):
        pass # do something useful

动态生成

from PyQt4 import uic
yourFormTypeInstance = uic.loadUi('/path/to/your/file.ui')

对于上面的(3),您最终将得到您为表单指定的任何基本类型的实例。然后,您可以根据需要重写 mousePressEvent

我建议您查看 PyQt4 参考手册中的第 13.1 节 。 13.2 节讨论了 uic 模块。

With PyQt there are three different ways to work with forms created in designer:

  1. Use single inheritance and make the form a member variable
  2. Use multiple inheritance
  3. Dynamically generate the members directly from the UI file

Single Inheritance:

class MyTableWidget(QTableWidget):
    def __init__(self, parent, *args):
        super(MyTableWidget, self).__init__(parent, args)
        self.ui = YourFormName()
        self.ui.setupUi(self)
        # all gui elements are now accessed through self.ui
    def mousePressEvent(self, event):
        pass # do something useful

Multiple Inheritance:

class MyTableWidget(QTableWidget, YourFormName):
    def __init__(self, parent, *args):
        super(MyTableWidget, self).__init__(parent, args)
        self.setupUi(self)
        # self now has all members you defined in the form
    def mousePressEvent(self, event):
        pass # do something useful

Dynamically Generated:

from PyQt4 import uic
yourFormTypeInstance = uic.loadUi('/path/to/your/file.ui')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文