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:

    tree widgets

    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.


  • Lifetime Qt Champion

    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?


  • Lifetime Qt Champion

    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.



  • I did it! I added the QDataWidgetMapper in the constructor of RowWidget. On creating the myWidget in the for loop, I commit the rowID of the row which is shown in the widget.

    Thanks to all!


  • Qt Champions 2016

    @Mr.-Kibu
    Have in mind that QDataWidgetMapper has a known bug/deficiency when working with relational sql model and having a combo box for the foreign key.



  • Thank you for the info, I don't know that. Where can I get more informations about this bug?


  • Qt Champions 2016

    @Mr.-Kibu
    Take a look at this thread.

    EDIT: Also this bug report.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.