簡體   English   中英

QT如何使用QTableView在QList中顯示QMap

[英]QT how to use QTableView to display a QMap within a QList

我有一個QList如下:

QList< QMap<QString, QString> > x;

例如:

table(1) <name<name(1),ABC> >

table(1) <age<age(1),10> >

我想將“名稱”和“年齡”作為列標題並將其值“ ABC”和“ 10”向下。 接下來,當我有:

table(1) <name<name(2),DFG> >

table(1) <age<age(2),20> >

下一行顯示值“ DFG”和“ 20”

所以,我該如何顯示呢?

為此,您必須將數據傳遞給模型,然后可以使用QTableView顯示數據。 要設置表的模型,請使用以下示例:

// 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);
    }
}

那么您應該將模型傳遞給表格視圖:

QTableView table;
table.setModel(table_model);
table.show();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM