Add items to columns in QStandardItemModel

ぐ巨炮叔叔 提交于 2019-12-19 10:14:35

问题


I am currently adding rows to my QTableView as such

QStandardItem* itm;
QStandardItemModel* model = new QStandardItemModel(this);
model->setColumnCount(2);
model->appendRow(new QStandardItem("Some Text in Column1");

How do I add items to column 2 dynamically by appending? In the above example column 2 is empty. How do I add item to column 2?


回答1:


Calling appendRow(QStandardItem *) only adds a single item to the first column. You would need to pass in a QList to appendRow() to add items to each column, e.g.:

QList<QStandardItem *> items;

items.append(new QStandardItem("Column 1 Text"));
items.append(new QStandardItem("Column 2 Text"));

QStandardItemModel* model = new QStandardItemModel(this);

model->setColumnCount(2);
model->appendRow(items);

See http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRow for more detail.



来源:https://stackoverflow.com/questions/36048581/add-items-to-columns-in-qstandarditemmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!