C++ 为树视图创建 Qt 模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1985936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Qt models for tree views
提问by Veeti
I'm writing an application in Qt (with C++) and I need to represent an object structure in a tree view. One of the ways to do this is to create a model for this, but I'm still quite confused after reading the Qt documentation about the subject.
我正在用 Qt(使用 C++)编写一个应用程序,我需要在树视图中表示一个对象结构。一种方法是为此创建一个模型,但在阅读了有关该主题的 Qt 文档后,我仍然很困惑。
The "structure" I have is pretty simple - there's a Project
object that holds Task
objects in a std::vector
container. These tasks can also hold child tasks.
我拥有的“结构”非常简单 - 有一个Project
对象将Task
对象保存在std::vector
容器中。这些任务也可以包含子任务。
I've already written methods to read & write these projects to/from XML files using Qt's XML classes.
我已经编写了使用 Qt 的 XML 类从 XML 文件读取和写入这些项目的方法。
Is there any more documentation or "recommended reading" for creating models from scratch? How do you recommend I start implementing this?
是否有更多文档或“推荐阅读”可以从头开始创建模型?你如何建议我开始实施这个?
回答by serge_gubenko
As an alternative to what was said by Virgil in a comment to the question, you could use QStandardItemModelclass for your model and just build your tree using this class. Below is an example:
作为 Virgil 在对该问题的评论中所说的替代方案,您可以将QStandardItemModel类用于您的模型,并使用此类构建您的树。下面是一个例子:
QStandardItemModel* model = new QStandardItemModel();
QStandardItem* item0 = new QStandardItem(QIcon("test.png"), "1 first item");
QStandardItem* item1 = new QStandardItem(QIcon("test.png"), "2 second item");
QStandardItem* item3 = new QStandardItem(QIcon("test.png"), "3 third item");
QStandardItem* item4 = new QStandardItem("4 forth item");
model->appendRow(item0);
item0->appendRow(item3);
item0->appendRow(item4);
model->appendRow(item1);
ui->treeView->setModel(model);
When the UI (view) is destroyed, delete model
. Documentation:
当 UI(视图)被销毁时,删除model
. 文档:
回答by e8johan
The basic trick to get this working is really to get the model to data structure mapping right. Something that might seem hard, but needn't be.
使这个工作的基本技巧是让模型正确映射到数据结构。一些看起来很难,但没必要的东西。
First, using the QAbstractItemModel::createIndexto build model indexes, you can refer to your own data structure through the pointer or uint32 that you can add to the index, depending on which instance of createIndex that you choose to use.
首先,使用QAbstractItemModel::createIndex构建模型索引,您可以通过添加到索引的指针或 uint32 来引用您自己的数据结构,具体取决于您选择使用哪个 createIndex 实例。
Second, having the structure clear in mind (as you seem to have), it is quite easy to write the parent
and index
functions. The key here is to understand that the model root is an unintialized QModelIndex
instance. I.e. QModelIndex::isValid() == false
indicates root.
其次,记住结构清晰(如您所见),编写parent
和index
函数非常容易。这里的关键是要理解模型根是一个未初始化的QModelIndex
实例。即QModelIndex::isValid() == false
表示根。
Third, if you go multi-column, remember that only the first column has children.
第三,如果你去多列,记住只有第一列有孩子。
Fourth, to check that you do things the expected way, do use the ModelTestclass. It monitors and checks your model, so that you follow the conventions that the Qt model view classes expect.
第四,要检查您是否按照预期的方式做事,请使用ModelTest类。它监视和检查您的模型,以便您遵循 Qt 模型视图类所期望的约定。