Results 1 to 3 of 3

Thread: QComboBox - Delegate - QDataWidgetMapper - QSqlRelationalTableModel

  1. #1
    Join Date
    Aug 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QComboBox - Delegate - QDataWidgetMapper - QSqlRelationalTableModel

    Hi, I'm new to Qt4. I'm using Qt4.5

    I use QSqlRelationalTableModel to show in a table view a database table with foreign keys to other tables. This works fine.
    I created a form to edit each table row. I used a mapper and connected table selection model signal to setCurrentModelIndex mapper slot, this is the code

    Qt Code:
    1. model = new QSqlRelationalTableModel(this, database);
    2. model->setTable("centros_programas");
    3. model->setRelation(1, QSqlRelation("provincias", "id_provincias", "nombre"));
    4.  
    5. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    6. model->select();
    7.  
    8. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Id"));
    9. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Provincia"));
    10. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Centro/Programa"));
    11. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Tipo"));
    12.  
    13. ui->comboBoxProvincia->setModel(model->relationModel(1) );
    14. ui->comboBoxProvincia->setModelColumn(model->relationModel(1)->fieldIndex("nombre"));
    15.  
    16. ui->table->setModel(model);
    17. ui->table->setColumnHidden(0, true);
    18.  
    19. mapper = new QDataWidgetMapper(this);
    20. mapper->setModel(model);
    21. mapper->addMapping(ui->comboBoxProvincia, model->fieldIndex("id_provincias") );
    22. mapper->addMapping(ui->lineEditCentro, model->fieldIndex("nombre") );
    23. mapper->addMapping(ui->lineEditTipo, model->fieldIndex("tipo") );
    24.  
    25. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    26.  
    27. connect(ui->table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    the thing is: in the form for editing each row, I have a combo box with data from a related table, as you can see in this line:

    Qt Code:
    1. ui->comboBoxProvincia->setModel(model->relationModel(1) );
    To copy to clipboard, switch view to plain text mode 

    I didn't create another model for the combo box.
    When I see it running it fills correctly the combo box, but it is not synchronized with data, it always shows first row of the related table.
    I was searching and it seems that I need a delegate for it. This is right? I will need a delegate for each combo box? What if I have two or more combo boxes in the form?
    Thanks in advance

  2. #2
    Join Date
    Aug 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: QComboBox - Delegate - QDataWidgetMapper - QSqlRelationalTableModel

    Amazing, I found the answer myself digging in Qt examples. Here is the code:

    Qt Code:
    1. model->setTable("centros_programas");
    2. model->setRelation(1, QSqlRelation("provincias", "id_provincias", "nombre"));
    3.  
    4. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5. model->select();
    6.  
    7. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Id"));
    8. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Provincia"));
    9. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Centro/Programa"));
    10. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Tipo"));
    11.  
    12. ui->comboBoxProvincia->setModel(model->relationModel(1) );
    13. ui->comboBoxProvincia->setModelColumn(model->relationModel(1)->fieldIndex("nombre"));
    14.  
    15. ui->table->setModel(model);
    16. ui->table->setColumnHidden(0, true);
    17.  
    18. ui->table->setItemDelegate( new QSqlRelationalDelegate( ui->table ));
    19.  
    20. mapper = new QDataWidgetMapper(this);
    21. mapper->setModel(model);
    22. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    23. mapper->addMapping(ui->comboBoxProvincia, 1);
    24. mapper->addMapping(ui->lineEditCentro, model->fieldIndex("nombre") );
    25. mapper->addMapping(ui->lineEditTipo, model->fieldIndex("tipo") );
    26. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    27.  
    28. connect(ui->table->selectionModel(), SIGNAL( currentChanged(QModelIndex,QModelIndex) ), mapper, SLOT( setCurrentModelIndex(QModelIndex) ));
    29.  
    30. ui->table->setCurrentIndex(model->index(0, 0));
    To copy to clipboard, switch view to plain text mode 

    I had to set a delegate in two places, the table view and the mapper.
    These two lines:
    Qt Code:
    1. ui->table->setItemDelegate( new QSqlRelationalDelegate( ui->table ));
    2.  
    3. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    To copy to clipboard, switch view to plain text mode 

    I hope this can help.

  3. #3
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox - Delegate - QDataWidgetMapper - QSqlRelationalTableModel

    Hi, Jorge.

    I tried to follow your code to fix my problem which seems to be similar to what you have.

    Here is my code:

    Qt Code:
    1. // Setup the model to use in the mapper
    2. model = new QSqlRelationalTableModel(this);
    3. model->setTable("Orders");
    4. //model->select();
    5. model->setRelation(7, QSqlRelation("customers", "CustomerNumber", "Name"));
    6. model->setRelation(5, QSqlRelation("employee", "EmployeeNumber", "UserName"));
    7.  
    8. Qt::SortOrder order = Qt::AscendingOrder;
    9. model->sort(0, order);
    10.  
    11. ui->customerComboBox->setModel(model->relationModel(7));
    12. ui->customerComboBox->setModelColumn(model->relationModel(7)->fieldIndex("Name"));
    13.  
    14. ui->employeeComboBox->setModel(model->relationModel(5));
    15. ui->employeeComboBox->setModelColumn(model->relationModel(5)->fieldIndex("LastName"));
    16.  
    17. // Setup the mapper for the order widgets
    18. mapper = new QDataWidgetMapper(this);
    19. mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    20. mapper->setModel(model);
    21. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    22. mapper->addMapping(ui->idLineEdit, 0);
    23. mapper->addMapping(ui->fullfilledCheckBox,1);
    24. mapper->addMapping(ui->fullfilledDateEdit, 2);
    25. mapper->addMapping(ui->orderDateEdit, 3);
    26. mapper->addMapping(ui->dueDateEdit, 4);
    27. mapper->addMapping(ui->employeeComboBox, model->fieldIndex("Employee_EmployeeNumber"), "currentIndex");
    28. mapper->addMapping(ui->dueTimeEdit, 6);
    29. mapper->addMapping(ui->customerComboBox, model->fieldIndex("customers_CustomerNumber"), "currentIndex");
    To copy to clipboard, switch view to plain text mode 

    but my combo boxes do not synchronize with the database. I see the values in the combo boxes but when the mapper loads it shows the first item always. And if I change an item and save it does not change the item.

    Any ideas where I am going wrong?

    Pericles

Similar Threads

  1. QItemDelegate, QDataWidgetMapper and QComboBox
    By cydside in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2009, 19:44
  2. QDataWidgetMapper and QComboBox
    By mazurekwrc in forum Qt Programming
    Replies: 0
    Last Post: 31st March 2009, 14:02
  3. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 18:53
  4. Again QTableWidget and QComboBox delegate
    By Aki-Matti in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2008, 14:40
  5. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 11:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.