来自模型的数据未插入 QTableView

Posted

技术标签:

【中文标题】来自模型的数据未插入 QTableView【英文标题】:Data from model not inserted in QTableView 【发布时间】:2011-08-04 08:47:34 【问题描述】:

我正在尝试在模型中的TableView 中插入一些数据,但我做错了,因为没有插入数据。不过,该表已更新为列和行。

所以我有一个GraphicsView,我在其中绘制了一些自定义GraphicsItems。每次向场景中添加新项目时,模型都应该更新并向我的TableView 发送信号以将数据也插入其中。

这里我在添加新项目时更新模型:

Clothoid *temp = new Clothoid(); temp->setStartPoint(p1); temp->setEndPoint(p2); clothoids.append(temp); 场景->添加项目(临时); model.setColumnCount(3); model.setRowCount(clothoids.size()); QModelIndex index = model.index(clothoids.size(), 1, QModelIndex()); model.setData(index,clothoids.last()->startCurvature); index = model.index(clothoids.size(), 2, QModelIndex()); model.setData(index,clothoids.last()->endCurvature); index = model.index(clothoids.size(), 3, QModelIndex()); model.setData(index,clothoids.last()->clothoidLength); 发出clothoidAdded(&model);

Clothoids 是我的自定义 graphicsItems 的列表:

QList &lt Clothoid *> 回旋曲线;

信号连接到我的主窗口中的插槽:

ui->setupUi(这个); SpinBoxDelegate 委托; ui->clothoidTable->setItemDelegate(&delegate); 连接(ui->graphicsView,SIGNAL(clothoidAdded(QStandardItemModel*)),ui->clothoidTable,SLOT(onClothoidAdded(QStandardItemModel*)));

插槽在哪里:

无效 TableViewList::onClothoidAdded(QStandardItemModel *model) 设置模型(模型);

我做错了什么?

【问题讨论】:

【参考方案1】:

您不想直接调用 setData()。以下是您需要采取的几个关键步骤:

您的模型应该包含一个包含Clothoid 指针的容器(可能是QList)(无论它是否负责释放资源)。容器的索引应该直接映射到它在视图中占据的行。

您的 data()setData() 需要正确实现,这样模型才能知道给定行的每个单元格中的 Clothoid 信息。他们应该在enum 上具有switch() 语句表示列号,如下所示:


// in data() after the usual error checking, etc
if(role == Qt::DisplayRole)
    
    Clothoid* cloth = myListOfClothoids.at(index.row());
    switch(index.column())
        
        // This enum is defined in the header for the Clothoid class 
        //  and represents the COLUMN NUMBER in which to show the data
        case Clothoid::START: 
            return cloth->startCurvature; // these probably shouldn't be public members btw
        case Clothoid::END:
            return cloth->endCurvature;
        case Clothoid::LENGTH:
            return cloth->clothoidLength;
        
    

// in setData()
if(role == Qt::DisplayRole)
    
    Clothoid* cloth = myListOfClothoids.at(index.row());
    switch(index.column())
        
        case Clothoid::START: 
            cloth->startCurvature = variant.toWhatever(); 
            break;
        case Clothoid::END:
            cloth->endCurvature = variant.toWhateverElse(); 
            break;
        case Clothoid::LENGTH:
            cloth->clothoidLength = variant.toYetSomethingElse();
            break;
        default:
            return false;
        
    emit dataChanged(index,index);
    return true;
    
    您的模型应该有一个addClothoid() 函数。在这个函数中,您想要执行以下操作:

int rowIndexFirst = 0; // insert into first row
int rowIndexLast = rowIndexFirst; // only inserting one row/Clothoid
beginInsertRows(QModelIndex(), rowIndexFirst, rowIndexLast);
myListOfClothoids.prepend(newClothoidPtr); // insert clothoid into first index, as well
endInsertRows(); // begin and end take care of signalling the view for you!

我真的建议这样做。是的,要重构到这个程度需要做很多工作,但它是值得的,相信我。

希望这会有所帮助。

【讨论】:

以上是关于来自模型的数据未插入 QTableView的主要内容,如果未能解决你的问题,请参考以下文章

来自前端的 Json 数据未通过后端使用 Knockout 和 MVC 插入到数据库中

来自模型的输入未包含在表单 POST 数据中

来自 JQuery 填充字段的值未绑定到 MVC 模型

分配角色未插入到其相应的模型

BigQuery:插入行,但未写入

来自表单的数据未使用 php 添加到数据库表中