qStyledItemdelegate / qabtractitemdelegate for Qlistview

发布于 2025-01-29 21:19:37 字数 435 浏览 2 评论 0 原文

我的目的是创建Contact App之类的内容,我可以在其中列出联系人并选择它以查看有关人的信息。我发现可能的解决方案之一是使用QListView + QStyleDitemdelegate / Qabstractitemdelegate。关于它的信息非常困难,因此我不清楚它。

(联系应看起来像

https://www.sketchappsources.com/free-source/4395-ios-contacts-screen-app-sketch-freebie-resource-resource.html )?

My aim is to create something like contact app, where I can list contacts and choose it to see information about person. I figure out that one of the possible solution is to use QListView + QStyledItemDelegate / QAbstractItemDelegate. The information about it is very difficult so I don't understand it clearly;

(Contact should look something like https://www.sketchappsources.com/free-source/4395-ios-contacts-screen-app-sketch-freebie-resource.html)

So how should I use QAbstractItemDelegate ( I heard that I must reimplement paintEvent )?

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

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

发布评论

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

评论(1

街道布景 2025-02-05 21:19:37

我建议您从数据模型开始。

  1. 使用 qstandardItemModel 类,用于开始并使用 qstandardItem 类实例填充它。它允许您为项目设置图标,文本,字体,背景,大小和其他属性。请参阅 https://doc.qt.io/qt-5/qt-5/qtand-5/qstand-candardemmodel。 html#详细信息
  2. 将您的模型设置为 qlistView 使用 setModel
  3. 要处理项目,请单击 qlistView 's 单击代码>信号。

要以更复杂的方式渲染项目,您应该

  1. 覆盖 qStyledIteMdelegate 类,它是 paint sizehint 方法。在 Paint 方法中,您应该实现渲染,并且您的 sizeHint 方法应返回项目的有效尺寸。请参阅 https://doc.qt.io/qt.io/qt-5/qt-5/qabstractitemdelemdelemdelegate。 html#详细信息
  2. 获取项目数据渲染使用 data qmodelindex的方法> qmodelindex 参考将传递给 paint 方法。使用不同的角色获取适当的数据。请参阅
  3. 通过将其设置为 QListView by setItemdelegate ,请使用您的委托类。

应将模型设置为QListView,并且以相同方式处理项目点击。

I suggest you to start with a data model.

  1. Use QStandardItemModel class for beginning and populate it with QStandardItem class instances. It would allow you to set icon, text, font, background, size and other properties for items. Refer to https://doc.qt.io/qt-5/qstandarditemmodel.html#details
  2. Set your model to QListView using setModel
  3. To handle items clicked connect to QListView's clicked signal.

To render items in more complex way you should

  1. Override QStyledItemDelegate class and it's paint and sizeHint methods. In the paint method you should implement rendering and your sizeHint method should return a valid size for items. Refer to https://doc.qt.io/qt-5/qabstractitemdelegate.html#details
  2. To get item data to render use data method of QModelIndex reference that is passed to paint method. Use different roles to get appropriate data. Refer to https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum
  3. Use your delegate class by setting it to QListView by setItemDelegate.

Model should be set to QListView and item clicks are handled in same way.

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