【问题标题】:Data from model not inserted in QTableView来自模型的数据未插入 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) { 设置模型(模型); }

我做错了什么?

【问题讨论】:

    标签: qt4 qtableview qstandarditemmodel


    【解决方案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;
        }
    
    1. 您的模型应该有一个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!
    

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      相关资源
      最近更新 更多