Python PyQT4 - 将未知数量的 QComboBox 小部件添加到 QGridLayout

发布于 2024-08-10 07:23:15 字数 393 浏览 10 评论 0 原文

我想从队列中检索人员姓名列表,并使用 addWidget() 函数将每个人的姓名复选框放置到 QGridLayout 中。我可以成功地将这些项目放入 QListView 中,但它们只是相互叠加而不是创建新行。有人对我如何解决这个问题有任何想法吗?

self.chk_People = QtGui.QListView()
items = self.jobQueue.getPeopleOffQueue()

for item in items:
    QtGui.QCheckBox('%s' % item, self.chk_People)

self.jobQueue.getPeopleOffQueue() 会返回类似 ['Bob', 'Sally', 'Jimmy'] 的内容(如果有帮助的话)。

I want to retrieve a list of people's names from a queue and, for each person, place a checkbox with their name to a QGridLayout using the addWidget() function. I can successfully place the items in a QListView, but they just write over the top of each other rather than creating a new row. Does anyone have any thoughts on how I could fix this?

self.chk_People = QtGui.QListView()
items = self.jobQueue.getPeopleOffQueue()

for item in items:
    QtGui.QCheckBox('%s' % item, self.chk_People)

self.jobQueue.getPeopleOffQueue() would return something like ['Bob', 'Sally', 'Jimmy'] if that helps.

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

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

发布评论

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

评论(2

跨年 2024-08-17 07:23:15

这一行:

QtGui.QCheckBox('%s' % item, self.chk_People)

不会将复选框添加到列表视图中,它仅以列表视图为父级来创建它,并且有很大的区别。

使用列表视图的最简单方法是 QListWidget 便利类。为此,将复选框创建为 QListWidgetItem 的实例,然后在列表小部件上使用 addItem 将它们真正添加到其中。

将它们添加到网格布局时是否遇到问题?通常,如果您拥有的复选框数量较小,则网格布局可能会更好 - 这完全取决于您希望应用程序的外观。但如果您可能有很多这样的对象,那么列表小部件/视图是最好的。

This line:

QtGui.QCheckBox('%s' % item, self.chk_People)

Doesn't add the check box to the list view, it only creates it with the list view as the parent, and there's a big difference.

The simplest way to use a list view is the QListWidget convenience class. For that, create your checkboxes as instances of QListWidgetItem and then use addItem on the list widget to really add them to it.

Did you have a problem adding them to a grid layout? Usually, if the amount of checkboxes you have is small a grid layout might be better - it all depends on how you want your app to look. But if you might have a lot of such objects, then a list widget/view is the best.

蒗幽 2024-08-17 07:23:15

我无法告诉您 PyQt 中的解决方案,但您需要遵循的结构如下。 QListView 可以做你需要的事情,你不需要创建单独的复选框。创建 QAbstractItemModelQStandardItemModel (取决于您想要执行的编码量)覆盖 < code>flags() 方法返回适当的标志包括 Qt::ItemIsUserCheckable,在 columncount() 方法中添加一个额外的列以包含复选框并在 data<您希望复选框出现的列的 /code> 方法返回 Qt::Checked 的选中状态,Qt::UncheckedQt::CheckStateRole

这也可以使用 QListWidget 来完成,您可以使用 QListWidgetItem 添加数据,不需要创建一个模型。在QListWidgetItem上,您可以使用setFlags()setData(QVariant(bool, Qt::CheckStateRole),而无需子类化模型

I can't tell you the solution in PyQt but the structure you need to follow is the following. The QListView can do what you need, you don't need to create separate checkboxes. Create a subclass of QAbstractItemModel or QStandardItemModel (depending on how much coding you will want to do) override the flags() method to return the appropriate flags including Qt::ItemIsUserCheckable, in the columncount() method add an extra column to include the checkbox and in the data method for the column where you want your checkbox to appear return the checked state Qt::Checked, Qt::Unchecked for the Qt::CheckStateRole.

This can also be accomplished using the QListWidget where you can use QListWidgetItem for adding data and do not need to create a model. On QListWidgetItem you can use setFlags() and setData(QVariant(bool, Qt::CheckStateRole) without having to subclass a model

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