[英]QT how to use QTableView to display a QMap within a QList
I am having a QList as follow: 我有一个QList如下:
QList< QMap<QString, QString> > x;
for example: 例如:
table(1) <name<name(1),ABC> >
table(1) <age<age(1),10> >
I would like to have the "name" and "age" as columns header and down to them the values "ABC" and "10". 我想将“名称”和“年龄”作为列标题并将其值“ ABC”和“ 10”向下。 Next when I have: 接下来,当我有:
table(1) <name<name(2),DFG> >
table(1) <age<age(2),20> >
the values "DFG" and "20" comes in the next row 下一行显示值“ DFG”和“ 20”
so, how can I do make this display ? 所以,我该如何显示呢?
To do this, you have to pass your data to a model and then you can use QTableView to display your data; 为此,您必须将数据传递给模型,然后可以使用QTableView显示数据。 to set a model for a table use the following sample: 要设置表的模型,请使用以下示例:
// creating a 4*4 table
QStandardItemModel* table_model = new QStandardItemModel(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem((QString())); // you should set your data here (in this case as a string)
table_model.setItem(row, column, item);
}
}
then you should pass the model to a tableview: 那么您应该将模型传递给表格视图:
QTableView table;
table.setModel(table_model);
table.show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.