如何使用QColumnView显示多列数据
我想在 QColumnView 的多列中显示数据。我使用 Qt Creator 和 Qt 4 进行开发。
考虑一个地址簿应用程序,其中您有多个组:组 1、组 2 等。您的联系人可以属于这些组中的任何一个。
Group 1:
John Smith
Pocahontas
Group 2:
Chief Powhatan
Group 3:
...
当选择第一列中的组时,第二列将显示该组中的所有联系人,当选择联系人时,其个人信息将显示在第三列中。
我已尝试以下操作(基于 Qt 文档中的示例):
QStringList strList1;
strList1 << "Group 1" << "Group 2" << "Group 3";
strListM1 = new QStringListModel(); // Previously declared as QStringListModel *strListM1
strListM1->setStringList(strList1);
ui->columnView->setModel(strListM1);
但是,我无法弄清楚如何添加更多列,以及如何将联系人姓名添加为第一列中这些组的子级。
我该怎么做?如何动态添加列和行(而不是像上面那样使用 QStringList 或任何其他类似的行方法)?
I want to display data in multiple columns in a QColumnView. I am using Qt Creator and Qt 4 for development.
Consider an address book application where you have multiple groups: Group 1, Group 2, etc. Your contacts can belong to any of those groups.
Group 1:
John Smith
Pocahontas
Group 2:
Chief Powhatan
Group 3:
...
When a group in the first column is selected, the second column will show all contacts in that group, and when a contact is selected, their personal information is shown in a third column.
I have tried the following (based on an example from Qt Documentation):
QStringList strList1;
strList1 << "Group 1" << "Group 2" << "Group 3";
strListM1 = new QStringListModel(); // Previously declared as QStringListModel *strListM1
strListM1->setStringList(strList1);
ui->columnView->setModel(strListM1);
However, I have not been able to figure out how to add more columns, and add the contact names as children of those groups in the first column.
How can I do this? How could I add columns and rows dynamically (instead of using the QStringList like above, or any other similar method for rows)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以依赖
QStandardItem
和QStandardItemModel
。下面是一个非常简单且可编译的示例,说明如何将这些类与QColumnView
一起使用:有关 Qt 模型/视图编程的更多详细信息,请参阅 官方文档。
You may rely on
QStandardItem
andQStandardItemModel
. Here's a really simple and compilable example on how to use these classes withQColumnView
:For more details abut Qt's Model/View Programming, please refer to the official documentation.