QTreeWidgetItem 中的角色是什么?

发布于 2024-07-25 20:34:31 字数 353 浏览 4 评论 0原文

我有一个带有多个列的 QTreeWidget,我向其中添加了 QTreeWidgetItems。 我尝试使第二列包含每个 Item 的数值,以便我可以按该值对项目进行排序,但

QTreeWidgetItem has a method called setData(int column, int role, QVariant(data))

我找不到任何有关此 role 参数的文档。 我所知道的是,如果我将其设置为 1 或 2,则列中会显示一些内容,如果我将其设置为 0 或 >=3,则列中不会显示任何内容,无论如何,数字最终总是按字母顺序排序,这是错误的。

I have a QTreeWidget with several Columns, I add QTreeWidgetItems to it. I try to make the second column contain a numerical value for each Item so I can sort the items by this value

QTreeWidgetItem has a method called setData(int column, int role, QVariant(data))

I cannot find any documentation on what this role argument is. All I know is that if I set it to 1 or 2, something shows up in the column, if I set it to 0 or >=3, nothing shows up in the column, regardless, the numbers always end up being sorted alphabetically, which is wrong.

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

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

发布评论

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

评论(3

作死小能手 2024-08-01 20:34:32

相关文档可以在 Qt::ItemDataRole 下找到(通过 QAbstractItemModel::setData 找到)。 角色用于指定您传递的数据的用途。 您可以使用不同的角色来设置项目的工具提示、字体或颜色等。

The relevant documentation can be found under Qt::ItemDataRole (found through QAbstractItemModel::setData). Roles are used to specify what the data you are passing should be used for. You can use different roles to set the tooltip, font or color of an item, among other things.

Spring初心 2024-08-01 20:34:32

请注意,item->text() 相当于 item->data(Qt::DisplayRole).toString()

Note that item->text() is a convenience equivalent to item->data(Qt::DisplayRole).toString()

狼性发作 2024-08-01 20:34:31

您可以将 Qt::UserRole 用于应用程序特定目的。
由于此数据是 QVariant,因此您可以创建一个 QList 来设置多个数据,然后将其转换为 QVariant 并设置数据。

这是一个例子:

QTreeWidgetItem* item = new QTreeWidgetItem();
QList<QVariant> dataList;
dataList.append("data 1");
dataList.append("data 2");
QVariant data(dataList);
item->setData(0, Qt::UserRole, data);

You can use the Qt::UserRole for application specific purposes.
Since this data is a QVariant, you can create a QList to set multiple data and after that cast it to QVariant and set the data.

Here is an example:

QTreeWidgetItem* item = new QTreeWidgetItem();
QList<QVariant> dataList;
dataList.append("data 1");
dataList.append("data 2");
QVariant data(dataList);
item->setData(0, Qt::UserRole, data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文