如何向 QTableWidget 插入数据?
我有这个应用程序,用户可以在 QGraphicsView 中绘制一些自定义 QGraphicsItems,我希望有关这些项目的一些数据也显示在 QTableWidget 中。
自定义QGraphicsItem的代码: 头文件:
class Clothoid : public QGraphicsItem { public: Clothoid(QPoint startPoint, QPoint endPoint); virtual ~Clothoid(); QPoint sPoint; QPoint ePoint; CFloat startCurvature; CFloat endCurvature; CFloat clothoidLength; CFloat tangentAngle; ... }
cpp 文件:
Clothoid::Clothoid(QPoint startPoint, QPoint endPoint) { sPoint = startPoint; ePoint = endPoint; startCurvature = 0.0; endCurvature = 0.0; clothoidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) + pow(endPoint.y() - startPoint.y(),2)); }
图形视图的代码:
renderArea::renderArea(QWidget *parent): QGraphicsView(parent) { scene = new QGraphicsScene(this); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setSceneRect(0, 0, 850, 480); setScene(scene); setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::Antialiasing); setTransformationAnchor(AnchorUnderMouse); scale(qreal(1.0), qreal(1.0)); setMinimumSize(400, 400); } void renderArea::mousePressEvent(QMouseEvent *event) { QPoint p = event->pos(); updateList(p); } void renderArea::updateList(const QPoint &p) { Point point; point.point = p; point.isSelected = false; list.append(point); if (list.size() > 1) updateClothoid(list[list.size()-2].point, list[list.size()-1].point); } void renderArea::updateClothoid(const QPoint &p1, const QPoint &p2) { Clothoid *temp = new Clothoid(p1, p2); clothoids.append(temp); scene->addItem(temp); emit clothoidAdded(&clothoids); }
其中 Clothoids 定义为:
QList clothoids;
我将信号与表小部件专用的另一个类中的插槽连接:
void TableViewList::onClothoidAdded(QList *clothoids) { setRowCount(clothoids->size()); for (int i = 0; i size(); i++){ setItem(i+1, 0, new QTableWidgetItem(clothoids->at(i)->startCurvature)); setItem(i+1, 1, new QTableWidgetItem(clothoids->at(i)->endCurvature)); setItem(i+1, 2, new QTableWidgetItem(clothoids->at(i)->clothoidLength)); setItem(i+1, 3, new QTableWidgetItem(clothoids->at(i)->sPoint.x() + ", " + clothoids->at(i)->sPoint.y())); setItem(i+1, 4, new QTableWidgetItem(clothoids->at(i)->ePoint.x() + ", " + clothoids->at(i)->ePoint.y())); } }
问题是数据未插入表中。我检查了调试,发现该数组包含所需的数据。我怎样才能正确访问它?有什么想法吗?
当尝试使用 QTableView 和 QStandardItemModel 时,我遇到这个问题:模型中的数据未插入表中:
renderingWidget::renderingWidget(QWidget *parent) : QWidget(parent), ui(new Ui::renderingWidget) { ui->setupUi(this); model.setColumnCount(3); ui->clothoidTable->setModel(&model); SpinBoxDelegate delegate; ui->clothoidTable->setItemDelegate(&delegate); connect (ui->saveButton, SIGNAL(clicked()), this, SLOT(createClothoid())); } void renderingWidget::createClothoid() { model.setRowCount(model.rowCount()+1); QModelIndex index = model.index(model.rowCount(), 1, QModelIndex()); model.setData(index, QVariant(ui->lengthSpinBox->value())); index = model.index(model.rowCount(), 2, QModelIndex()); model.setData(index, QVariant(ui->sCurvSpinBox->value())); index = model.index(model.rowCount(), 3, QModelIndex()); model.setData(index, QVariant(ui->eCurvSpinBox->value())); ui->clothoidTable->setModel(&model); }
我希望能够将数据插入到某些文本框/旋转框中,然后单击按钮,应将数据添加到桌子。但仅更新行数,而不更新其中的数据。我在为模型设置数据时做错了什么吗?
I have this application where the user can draw some custom QGraphicsItems in a QGraphicsView and I would like that some data about those items be also displayed in a QTableWidget.
The code for the custom QGraphicsItem:
header file:
class Clothoid : public QGraphicsItem { public: Clothoid(QPoint startPoint, QPoint endPoint); virtual ~Clothoid(); QPoint sPoint; QPoint ePoint; CFloat startCurvature; CFloat endCurvature; CFloat clothoidLength; CFloat tangentAngle; ... }
cpp file:
Clothoid::Clothoid(QPoint startPoint, QPoint endPoint) { sPoint = startPoint; ePoint = endPoint; startCurvature = 0.0; endCurvature = 0.0; clothoidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) + pow(endPoint.y() - startPoint.y(),2)); }
The code for the Graphics view:
renderArea::renderArea(QWidget *parent): QGraphicsView(parent) { scene = new QGraphicsScene(this); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setSceneRect(0, 0, 850, 480); setScene(scene); setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::Antialiasing); setTransformationAnchor(AnchorUnderMouse); scale(qreal(1.0), qreal(1.0)); setMinimumSize(400, 400); } void renderArea::mousePressEvent(QMouseEvent *event) { QPoint p = event->pos(); updateList(p); } void renderArea::updateList(const QPoint &p) { Point point; point.point = p; point.isSelected = false; list.append(point); if (list.size() > 1) updateClothoid(list[list.size()-2].point, list[list.size()-1].point); } void renderArea::updateClothoid(const QPoint &p1, const QPoint &p2) { Clothoid *temp = new Clothoid(p1, p2); clothoids.append(temp); scene->addItem(temp); emit clothoidAdded(&clothoids); }
where clothoids are defined as:
QList clothoids;
I connect the signal with the slot in another class special for the table widget:
void TableViewList::onClothoidAdded(QList *clothoids) { setRowCount(clothoids->size()); for (int i = 0; i size(); i++){ setItem(i+1, 0, new QTableWidgetItem(clothoids->at(i)->startCurvature)); setItem(i+1, 1, new QTableWidgetItem(clothoids->at(i)->endCurvature)); setItem(i+1, 2, new QTableWidgetItem(clothoids->at(i)->clothoidLength)); setItem(i+1, 3, new QTableWidgetItem(clothoids->at(i)->sPoint.x() + ", " + clothoids->at(i)->sPoint.y())); setItem(i+1, 4, new QTableWidgetItem(clothoids->at(i)->ePoint.x() + ", " + clothoids->at(i)->ePoint.y())); } }
The problem is that the data isn't inserted in the table. I checked with debugging and I saw that the array holds the wanted data. How could I access it correctly? Any ideas?
When trying with QTableView and QStandardItemModel I encounter this problem: the data in the model is not inserted in the table:
renderingWidget::renderingWidget(QWidget *parent) : QWidget(parent), ui(new Ui::renderingWidget) { ui->setupUi(this); model.setColumnCount(3); ui->clothoidTable->setModel(&model); SpinBoxDelegate delegate; ui->clothoidTable->setItemDelegate(&delegate); connect (ui->saveButton, SIGNAL(clicked()), this, SLOT(createClothoid())); } void renderingWidget::createClothoid() { model.setRowCount(model.rowCount()+1); QModelIndex index = model.index(model.rowCount(), 1, QModelIndex()); model.setData(index, QVariant(ui->lengthSpinBox->value())); index = model.index(model.rowCount(), 2, QModelIndex()); model.setData(index, QVariant(ui->sCurvSpinBox->value())); index = model.index(model.rowCount(), 3, QModelIndex()); model.setData(index, QVariant(ui->eCurvSpinBox->value())); ui->clothoidTable->setModel(&model); }
I want to be able to insert the data in some text boxes/spin boxes and then on button click the data should be added in the table. But only the number of rows is updated not the data within. Am I doing anything wrong while setting the data for the model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能很难听,但我会放弃 QTableWidget 便利类。
查看有关 Qt 模型/视图编程的链接了解 Qt 的工作原理确实是为了处理像你这样的复杂表格。
我对模型/视图的两点意见是:
QTableView
而不是QTableWidget
。QAbstractItemModel
并实现data()
(用于读取),以及文档中您需要的所有其他功能。这是
最棘手的部分,但请参阅上面的链接以了解如何进行的演练
来做到这一点。
QTableView
的setModel()
到您的子类模型。如果您有更多问题,我很乐意回答。
This is going to be hard to hear, but I'd back away from the
QTableWidget
convenience class.Check this link on Qt Model/View Programming out for how Qt is really meant to handle complex tables like yours.
My two cents on Model/Views is this:
QTableView
instead of aQTableWidget
.QAbstractItemModel
and implementdata()
(for reading), andall the other functions you need from the documentation. This is the
trickiest part, but refer to the above link for a walkthrough of how
to do this.
setModel()
of theQTableView
to your subclassed model.If you have more questions, I'll be happy to answer them.