0
我有這個應用程序,用戶可以在QGraphicsView中繪製一些自定義的QGraphicsItems,我希望這些項目的一些數據也顯示在QTableWidget中。如何將數據插入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); }
其中迴旋曲線被定義如:
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); }
我希望能夠在某些文本框/旋轉框插入數據然後點擊按鈕,數據應該添加到表格中。但是隻有行數被更新而不是內部的數據。在爲模型設置數據時我做了什麼錯誤?
我試過qtableview,但我有一個問題,因爲我在模型中添加的數據沒有插入到表中。我爲此更新了我的問題 – schmimona