PyQt QTableWidgetItem 子类的 Init 方法

发布于 2024-10-21 18:45:44 字数 390 浏览 2 评论 0原文

我想对 QTableWidgetItem 进行子类化,以便我的子类可以有一个额外的“id”属性。

不幸的是,我有点困惑,因为 PyQt 文档 here 显示四种不同的 init 方法。

要为我的“QTableWidgetItemWithId”子类创建我自己的 init 方法(它也会传递额外的“id”参数),我应该使用 *args、**kwargs 吗?如果是这样,正确的语法是什么?

非常感谢

I'd like to subclass the QTableWidgetItem so that my subclass can have an additional 'id' attribute.

Unfortunately, I'm a little confused because the PyQt docs here show four different init methods.

To create my own init method for my "QTableWidgetItemWithId" subclass (which would also pass in an extra 'id' argument), should I be using *args, **kwargs? If so, what would be the correct syntax?

Thanks very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

乖乖哒 2024-10-28 18:45:44

我想说,这在一定程度上取决于您的品味和您认为可读的内容,以及您想要保持子类的灵活性。
您可以将任何您想要的内容添加到构造函数中,然后将所需的内容传递给 QTableWidgetItem 的构造函数。有很多方法可以做到这一点。我个人尽量避免 *args 和 **kwargs。

from PyQt4.QtGui import QTableWidgetItem

class ATableWidgetItem(QTableWidgetItem):

    def __init__(self, id, *args, **kwargs):
        super(ATableWidgetItem, self).__init__(*args, **kwargs)
        self.id = id

a = ATableWidgetItem(0)
b = ATableWidgetItem(0, QTableWidgetItem.UserType)
c = ATableWidgetItem(0, "text", type=QTableWidgetItem.UserType) 

class AnotherTableWidgetItem(QTableWidgetItem):

    def __init__(self, id):
        super(AnotherTableWidgetItem, self).__init__()
        self.id = id

d = AnotherTableWidgetItem(0)

class YetAnotherTableWidgetItem(QTableWidgetItem):

    def __init__(self, id, text="", type=QTableWidgetItem.Type):
        super(YetAnotherTableWidgetItem, self).__init__(text, type)
        self.id = id

e = YetAnotherTableWidgetItem(0)
f = YetAnotherTableWidgetItem(0, "Hello")
g = YetAnotherTableWidgetItem(0, "Hello", QTableWidgetItem.UserType)  

I'd say that depends a little on your taste and what you consider readable and also how flexible you want to keep your subclass.
You can add whatever you wish to your constructor and just hand what's needed to QTableWidgetItem's constructor. There are many ways of doing it. I personally try to avoid *args and **kwargs when possible.

from PyQt4.QtGui import QTableWidgetItem

class ATableWidgetItem(QTableWidgetItem):

    def __init__(self, id, *args, **kwargs):
        super(ATableWidgetItem, self).__init__(*args, **kwargs)
        self.id = id

a = ATableWidgetItem(0)
b = ATableWidgetItem(0, QTableWidgetItem.UserType)
c = ATableWidgetItem(0, "text", type=QTableWidgetItem.UserType) 

class AnotherTableWidgetItem(QTableWidgetItem):

    def __init__(self, id):
        super(AnotherTableWidgetItem, self).__init__()
        self.id = id

d = AnotherTableWidgetItem(0)

class YetAnotherTableWidgetItem(QTableWidgetItem):

    def __init__(self, id, text="", type=QTableWidgetItem.Type):
        super(YetAnotherTableWidgetItem, self).__init__(text, type)
        self.id = id

e = YetAnotherTableWidgetItem(0)
f = YetAnotherTableWidgetItem(0, "Hello")
g = YetAnotherTableWidgetItem(0, "Hello", QTableWidgetItem.UserType)  
吃不饱 2024-10-28 18:45:44

我相信您可以为您的小部件项目定义尽可能多的构造函数,只要它适合您。覆盖的基本方法是克隆

Qt 有一个带有自定义 QTableWidgetItem 后代的演示项目:带有自定义的电子表格 QTableWidgetItem 在这里定义: spreadsheetitem.h

希望这有帮助,问候

I believe you can define as many constructors to your widget item as it suits you. The essential method to override would be the clone.

Qt has a demo project with a custom QTableWidgetItem descendant here: Spreadsheet with custom QTableWidgetItem defined here: spreadsheetitem.h

hope this helps, regards

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