简体   繁体   English

在QStandardItemModel / QTreeView中处理数据?

[英]Manipulating data in a QStandardItemModel/QTreeView?

In my project, I parse an NBT data file and create a QStandardItemModel based off that file which is then displayed in a QTreeView. 在我的项目中,我解析一个NBT数据文件并基于该文件创建一个QStandardItemModel,然后将其显示在QTreeView中。 The model gets created fine and I know how to set certain parameters for each QStandardItem when it is created. 模型创建良好,我知道如何在创建每个QStandardItem时为其设置某些参数。 For example, I know how to set the display text and the icon. 例如,我知道如何设置显示文本和图标。 Is there a way I can create "custom containers" for storing "extra" data in each item? 有没有一种方法可以创建用于在每个项目中存储“额外”数据的“自定义容器”?

READING from a data file and parsing it into a model I (believe) I can do just fine. 从数据文件读取并将其解析为模型I(相信),我可以做的很好。 However the user needs to be able to edit/manipulate the model from within the QTreeView. 但是,用户需要能够在QTreeView中编辑/操作模型。 This includes adding and removing items . 这包括添加和删​​除项目 This seems simple enough HOWEVER, some of the data associated with that item is NOT displayed in the QTreeView. 这似乎很简单,但是与该项目关联的某些数据未显示在QTreeView中。 It's kinda... "hidden" until the user tries to edit it. 有点...“隐藏”,直到用户尝试对其进行编辑。 The user needs to double-click on an item to bring up a dialog where they can edit a whole bunch of other parameters and data (Like really long strings and stuff, stuff I can't just display in the QTreeView). 用户需要双击一个项目以打开一个对话框,在其中可以编辑大量其他参数和数据(就像很长的字符串和内容,我不能只在QTreeView中显示这些内容)。

I'm having significant difficulties trying to find a way to store all the "extra data" pertaining to each item in the tree. 我在寻找一种方法来存储与树中每个项目有关的所有“额外数据”时遇到了很大的困难。 Initially, a QVector of sorts pops into mind, however manipulating the model while also manipulating the QVector is a technique I just can't wrap my head around. 最初,人们会想到各种各样的QVector,但是在操纵模型的同时还要操纵QVector是我无法解决的技术。

To make things even worse, the user needs to be able to switch between different data files (aka models) while still retaining any edits made to the previous data file. 更糟的是,用户需要能够在不同的数据文件(即模型)之间切换,同时仍保留对先前数据文件所做的任何编辑。

Any ideas? 有任何想法吗? If you have any questions, don't hesitate to ask. 如有任何疑问,请随时提出。 I can clarify as much as you want. 我可以根据您的需要进行澄清。 :) Thanks for your time. :) 谢谢你的时间。

The answer is yes, you can store additional data. 答案是肯定的,您可以存储其他数据。 You need to setData() to the specific user role + 1 . 您需要将setData()设置为特定的user role + 1 For example: 例如:

view->model()->setData(someIndex,"New Data", Qt::UserRole + 1);

To get this data use data() method and same role. 要获取此数据,请使用data()方法和相同的角色。 For editing this you can also try to use custom dialog or custom delegate. 为了进行编辑,您还可以尝试使用自定义对话框或自定义委托。

QVariant can use containers such as QList or QStringList , so you can use containers too. QVariant 可以使用 QListQStringList QList 容器 ,因此您也可以使用容器。 For example: 例如:

auto in = ui->tableView->model()->index(0,0);
QList<QVariant> lst;
lst << "one" << "two" << "three";
view->model()->setData(in,QVariant(lst),Qt::UserRole+1);
//...
qDebug() << "output:"<<view->model()->data(in,Qt::UserRole+1).toList();

Output: 输出:

output: (QVariant(QString, "one") , QVariant(QString, "two") , QVariant(QString, "three") ) 输出:(QVariant(QString,“ one”),QVariant(QString,“ two”),QVariant(QString,“ three”)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM