简体   繁体   English

有没有一种方法可以将QTableView模型转换为QStandardItemModel

[英]Is there a way to Convert QTableView model to QStandardItemModel

As you know QTableView->model() returns QAbstractItemModel . 如您所知, QTableView->model()返回QAbstractItemModel is there a way to fill an instance of QStandardItemModel with QTableView->model() ? 有没有办法用QTableView->model()填充QStandardItemModel的实例? or convert QAbstractItemModel to QStandardItemModel ? 或者将QAbstractItemModel转换为QStandardItemModel i need some of QStandardItemModel mothods like clear() and there is no such thing in QAbstractItemModel . 我需要像clear()这样的QStandardItemModel方法,而QAbstractItemModel没有这样的东西。

QStandardItemModel *model = new QStandardItemModel();
model = tblView->model();

error: invalid conversion from 'QAbstractItemModel*' to 'QStandardItemModel*' [-fpermissive]

For create table i use this code: 对于创建表,我使用以下代码:

QStandardItemModel *model = new QStandardItemModel(0, 3, this);

for (int = 0; i < 100; i++) {
    model->setRowCount(model->rowCount() + 1);
    model->setData(model->index(i, 0), "...");
    model->setData(model->index(i, 1), "...");
    model->setData(model->index(i, 2), "...");
}
tblView->model() = model;

If that model is different from QStandardItemModel then you must create your own clear() method so you should provide the code for that model. 如果该模型与QStandardItemModel不同,则必须创建自己的clear()方法,以便提供该模型的代码。 On the other hand, if it is a QStandardItemModel you must cast it: 另一方面,如果它是QStandardItemModel ,则必须QStandardItemModel它:

model = qobject_cas<QStandardItemModel *>(tblView->model());

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

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