模型中的数据未插入 QTableView 中

发布于 2024-11-28 05:42:55 字数 1498 浏览 1 评论 0原文

我正在尝试从模型在 TableView 中插入一些数据,但我做错了什么,因为数据未插入。不过,该表已使用列和行进行更新。

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

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

    Clothoid *temp = new Clothoid();
        temp->setStartPoint(p1);
        temp->setEndPoint(p2);

        clothoids.append(temp);

        scene->addItem(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);

        emit clothoidAdded(&model);


Clothoids 是我的自定义图形项目的列表:

QList < Clothoid *> clothoids;

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

   

    ui->setupUi(this);    
        SpinBoxDelegate delegate;
        ui->clothoidTable->setItemDelegate(&delegate);

        connect(ui->graphicsView, SIGNAL(clothoidAdded(QStandardItemModel*)), ui->clothoidTable, SLOT(onClothoidAdded(QStandardItemModel*)));


插槽在哪里:

    void TableViewList::onClothoidAdded(QStandardItemModel *model)
        {
            setModel(model);
        }

我做错了什么?

I am trying to insert some data in a TableView from a model but I am doing something wrong because the data is not inserted. The table is updated with the columns and rows though.

So I have a GraphicsView where I am drawing some custom GraphicsItems. Each time a new Item is added to the scene the model is supposed to get updated and send a signal to my TableView to insert the data in it as well.

Here I update the model when the new item is added:


    Clothoid *temp = new Clothoid();
        temp->setStartPoint(p1);
        temp->setEndPoint(p2);

        clothoids.append(temp);

        scene->addItem(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);

        emit clothoidAdded(&model);


Clothoids being a list of my custom graphicsItems:


QList < Clothoid *> clothoids;

The signal is connected to the slot in my main window:

   

    ui->setupUi(this);    
        SpinBoxDelegate delegate;
        ui->clothoidTable->setItemDelegate(&delegate);

        connect(ui->graphicsView, SIGNAL(clothoidAdded(QStandardItemModel*)), ui->clothoidTable, SLOT(onClothoidAdded(QStandardItemModel*)));


where the slot is:


    void TableViewList::onClothoidAdded(QStandardItemModel *model)
        {
            setModel(model);
        }

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2024-12-05 05:42:56

您不想直接调用 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. 您的模型应该有一个 addCothoid () 函数。 在此函数中,您想要执行以下操作:

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!

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

希望这有帮助。

You don't want to be calling setData() directly. Here's a few key steps you need to take:

  • Your model should hold a container (QList, perhaps) of Clothoid pointers (whether it is responsible for freeing the resources or not). The index into the container should map directly to what row it occupies in the view.

  • Your data() and setData() need to be implemented correctly, such that the model knows what Clothoid info goes in each cell for a given row. They should have switch() statements on an enum representing column number, like this:


// 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. Your model should have an addClothoid() function. In this function you want to do something like:

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!

I really suggest doing this. Yes, its a lot of work to refactor to this extent, but its worth it, trust me.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文