Map multiple rows with multiple widgets
-
Hi,
I have got a QSqlTableModel with multiple rows (e.g. 3 rows). Now I want to map the data with multiple widgets. Each row should map to one widget, the mapped widget should be placed horizontal side by side. It should look like this:
I create the widgets side by side like this:
numberOfRows = model->rowCount(); // add a widget for each row for (int i=0;i<numberOfRows;++i) { myWidget = new RowWidget(); ui->QLayout->addWidget(myWidget); }
How do I map the data with the widgets, to show in one widget the data of one row (and if I edit the data in the widget the result will be submited to the database)
Thank you!
Franz
-
I would do it in the myWidget constructor, just add parameters for everything you need, assuming the link you provided constructor canlook like:
explicit myWidget(QString row1, int row2, double row2, QWidget* parent = 0);
and than just bind data when creating them in
for
loop. -
Hi,
Looks like a job for QDataWidgetMapper
-
Yes, I thought also in this direction. I have tried this:
numberOfRows = model->rowCount(); mapper = new QDataWidgetMapper(this); mapper->setModel(model); mapper->setItemDelegate(new QSqlRelationalDelegate(mapper)); // add a widget for each row for (int i=0;i<numberOfRows;++i) { myWidget = new RowWidget(); ui->QLayout->addWidget(myWidget); mapper->addMapping(myWidget->SpinBox,2); mapper->addMapping(myWidget->DoubleSpinBox,3); } mapper->toFirst();
But the code above did not worked correct (I have in every widget the data of the last row!!). How do I install the mapper to have in every widget the data of one row?
-
Use one mapper per RowWidget.
One solution would be to have the mapper itself in RowWidget, have a setModel and a setRow method also in RowWidget so you can build them and change the row used by the mapper as you wish.
-
@Mr.-Kibu
Take a look at this thread.EDIT: Also this bug report.