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?
This is going to be hard to hear, but I’d back away from the
QTableWidgetconvenience 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:
QTableViewinstead of aQTableWidget.QAbstractItemModeland 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 theQTableViewto your subclassed model.If you have more questions, I’ll be happy to answer them.