模型中的数据未插入 QTableView 中
我正在尝试从模型在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不想直接调用 setData()。以下是您需要采取的几个关键步骤:
您的模型应该包含一个
Clothoid
指针的容器(也许是 QList)(无论它是否负责释放资源)或不)。容器的索引应该直接映射到它在视图中占据的行。您的
data()
和setData()
需要正确实现,以便模型知道每个单元格中包含哪些 Clothoid 信息对于给定的行。它们应该在enum
上有switch()
语句,代表列号,如下所示:addCothoid ()
函数。 在此函数中,您想要执行以下操作:我真的建议这样做。是的,重构到这个程度需要做很多工作,但这是值得的,相信我。
希望这有帮助。
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()
andsetData()
need to be implemented correctly, such that the model knows what Clothoid info goes in each cell for a given row. They should haveswitch()
statements on anenum
representing column number, like this:addClothoid()
function. In this function you want to do something like: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.