Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDataWidgetMapper QCombobox wrong item selected

QDataWidgetMapper QCombobox wrong item selected

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    a.dernnell
    wrote on last edited by
    #1

    Hi,

    I try to edit a sqlite database with a form. I followed this post : "Mapping Data to Widgets":http://doc.qt.digia.com/qq/qq21-datawidgetmapper.

    The QCombobox is correctly populated with the "nom" field from "familles" table as follow :
    @m_relModel = new QSqlRelationalTableModel(this,db);
    m_relModel->setTable("clients");
    m_relModel->setRelation(9,QSqlRelation("familles","id","nom"));
    m_famillesModel = m_relModel->relationModel(9);
    m_relModel->select();
    ui->familleComboBox->setModel(m_famillesModel);
    ui->familleComboBox->setModelColumn(m_famillesModel->fieldIndex("nom"));@

    The problem is the QCombobox mapping.
    My QCombobox is editable and the right text is shown, but the wrong item is selected.
    I mean "Item 3" is written in the editable combobox for instance, but when I open the combobox popup, "Item 0" is selected. If I open a second time the popup, "Item 3" is selected.

    • If I call QDataWidgetMapper::submit() without opening the QCombobox popup, the first item is submitted, and the data change in the database even if nothing changed in the form. (That's the problem here)

    • If I've open two times the QCombobox popup (without selecting item) before submitting, the selected item (i.e the database model's item) is submitted, so it's OK.

    • And if I select another item, the selected item is submitted, it's OK too.

    I don't want to be forced to select the correct item again each time...

    I don't know if this is a bug, or if I do do things wrong.
    If someone could help me, it would be great.

    Here is my code :
    @if(!DatabaseManager::instance()->isConnected())
    DatabaseManager::instance()->connect();
    QSqlDatabase db = DatabaseManager::instance()->getDatabase();

    m_clientsModel = new QSqlTableModel(this,db);
    m_clientsModel->setTable("clients");
    m_clientsModel->select();
    
    m_relModel = new QSqlRelationalTableModel(this,db);
    m_relModel->setTable("clients");
    m_relModel->setRelation(9,QSqlRelation("familles","id","nom"));
    m_famillesModel = m_relModel->relationModel(9);
    m_relModel->select();
    ui->familleComboBox->setModel(m_famillesModel);
    ui->familleComboBox->setModelColumn(m_famillesModel->fieldIndex("nom"));
    
    m_fidelitesModel = new QSqlQueryModel;
    QSqlQuery query;
    query.prepare("select f.id as id_fidelite, c.nom || \" \" || c.prenom as nom from clients as c join fidelites as f on c.id_fidelite=f.id where c.id_famille = :id_famille");
    int famille_index = ui->familleComboBox->currentIndex();
    QVariant id_famille = m_famillesModel->data(m_famillesModel->index(famille_index,0));
    qDebug() << id_famille;
    query.bindValue(":id_famille",id_famille.toInt());
    query.exec&#40;&#41;;
    qDebug() << query.lastError(); // QSqlError(-1, "", "")
    m_fidelitesModel->setQuery(query);
    ui->fideliteComboBox->setModel(m_fidelitesModel);
    ui->fideliteComboBox->setModelColumn(1);
    
    m_mapper = new QDataWidgetMapper(this);
    m_mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    m_mapper->setModel(m_relModel);
    m_mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    m_mapper->addMapping(...)
    ...
    m_mapper->addMapping(ui->familleComboBox,9);
    ...
    m_mapper->setCurrentIndex(2);
    

    @

    for submitting :
    @void MyClass::on_pushButton_pressed()
    {
    qDebug() << m_mapper->submit(); // return false even if data are submitted to the database... (?)
    }@

    Thanks

    1 Reply Last reply
    0
    • A Offline
      A Offline
      a.dernnell
      wrote on last edited by
      #2

      OK, when I select my item manually, it works :
      @int id_famille = m_clientsModel->data(m_clientsModel->index(2,9)).toInt(); ui->familleComboBox->setCurrentIndex(getIndexFromFamilleId(id_famille));@
      with :
      @int ClientEditView::getIndexFromFamilleId(int id_famille)
      {
      int nbRows = m_famillesModel->rowCount();
      for(int i=0;i<nbRows;i++)
      {
      int temp = m_famillesModel->record(i).value(0).toInt();
      if(temp == id_famille)
      return i;
      }
      return -1;
      }@

      I think it's a bit tedious, but I haven't found other solution.
      However, @m_mapper->submit();@ always return false, somebody know why ?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved