Results 1 to 3 of 3

Thread: QComboBox - Delegate - QDataWidgetMapper - QSqlRelationalTableModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

    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, 18:44
  2. QDataWidgetMapper and QComboBox
    By mazurekwrc in forum Qt Programming
    Replies: 0
    Last Post: 31st March 2009, 13:02
  3. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 17:53
  4. Again QTableWidget and QComboBox delegate
    By Aki-Matti in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2008, 13:40
  5. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10: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
  •  
Qt is a trademark of The Qt Company.