QListWidget 或 QListView 与 QItemDelegate?
假设我需要显示项目列表。每个项目都包含一个 QPushButton、一个图像和一些文本。当用户单击按钮时,应该会发生一些事情(即我需要获取信号)。在 Qt 中实现这个的正确方法是什么?
经过一番阅读后,我明白如果我使用 QListWidget 和 QListWidgetItem,就可以实现这一点。我可以根据我的需要对每个 QListWidgetItem 进行子类化,并将它们设置在列表小部件中。
但是,我还了解到更合适的方法(模型视图方法)是使用 QlistView 与 QItemDelegate 结合使用。但如果我使用 QItemDelegate,似乎我只能绘制小部件。如何获取按钮事件?
很抱歉这个巨大的帖子。我对何时使用 QListWidget / QListView 的整个概念有点困惑。
Let's say I need to display a list of items. Each item contains a QPushButton an image and some text. When a user clicks on the button something should happen (ie I need to get the signal). What is the right way to implement this in Qt?
After some reading, I understand that if I use a QListWidget and QListWidgetItem, this can be achieved. I can subclass each QListWidgetItem according to my needs and set them in the list widget.
However, I also read that a more appropriate approach (the Model View approach) is to use a QlistView coupled with a QItemDelegate. But if I'm using QItemDelegate, it seems that I can only paint the widgets. How can I get the push button event?
Sorry for this huge post. I'm kind of confused about the whole concept of when to use a QListWidget / QListView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QListWidget
本质上是QListView
的自定义版本,专为列表小部件的标准情况而设计,当您所做的只是在列表中呈现图像或文本项及其关系时与底层模型的关系很简单。使用
QListWidget
及其关联的类QListWidgetItem
,您可以非常轻松地插入和删除项目。但是,如果您要插入 QPushButtons,则无法使用它,因此您不妨使用 QListView 及其(继承的)方法来为给定的设置小部件指数。至于信号,由于您将创建 QPushButton,因此只需使用它
来处理它。
QListWidget
is essentially a customized version ofQListView
, designed for standard cases of list widgets, when all you're doing is just presenting image or text items in a list and the relationship with the underlying model is straightforward.With
QListWidget
and its associated classQListWidgetItem
you can e.g. insert and remove items very easily. But if you're insertingQPushButtons
, then you can't use this so you might as well just useQListView
and its (inherited) methods for setting the widget for a given index.As for the signal, since you'll be creating the
QPushButton
s, just useto deal with that.
与马特的答案不同,您似乎可以在
QListWidgetItem
上使用自己的小部件,正如 qt-project.org 上的这篇文章所指出的:http://qt-project.org/forums/viewthread/17953。如果您只有几个项目要显示并且对构建模型类的麻烦不感兴趣,这可能会很有用。
To differ from Matt's answer, it seems you can use your own widget on a
QListWidgetItem
, as pointed out in this post on qt-project.org: http://qt-project.org/forums/viewthread/17953.This might be useful if you only have a few items to display and are not interested in the bother of cooking up a model class.