繁体   English   中英

C ++-QListWidget

[英]c++ - QListWidget

我创建了一个具有多个项目的有效的QListWidget,但我不知道如何使其变得用户友好。 看起来像这样:

1000
1001
1002
...

但我希望它看起来像这样,第4个数字有意义,而其余所有信息仅供用户使用。

1000 Name LastName and some other helpful info
1001 tom jeff smallville
1002 ming vase, 1992
...

例如,这行

fotoId = ui->devices->currentItem()->text().toInt();

在两种情况下都应该给我相同的结果。

只需将QAbstractItemModel :: setData()与自定义角色ID一起使用即可。

const int CustomRole = Qt::UserRole + 1; // enum should be better for multiple user role
...
// set values
listWidget->model()->setData(index1, 1000, CustomRole);
listWidget->model()->setData(index2, 1000, CustomRole);
...
// get value
int value = listWidget->model()->data(index1, CustomRole).toInt(); // should be 1000

您可能需要将字符串存储为想要在小部件中显示的字符串,并在检索字符串时解析结果,或者自己实现data()方法(使用Qt :: DisplayRole)。

暂无
暂无

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

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