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. ComboBox delegate on QSqlRelationalTableModel: setModelData?
QtWS25 Last Chance

ComboBox delegate on QSqlRelationalTableModel: setModelData?

Scheduled Pinned Locked Moved Solved General and Desktop
comboboxdelegaterelationaltable
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.
  • S Offline
    S Offline
    see_mountains
    wrote on 13 Jun 2019, 13:38 last edited by
    #1

    Hi,

    I'm fairly new to Qt and the whole C++ stuff and need a little help. I think the following is a common issue, but I couldn't find the last part of the puzzle so I appreciate any help or push in the right direction.

    I'm writing a database frontend applikation with mariaDB/mySQL backend and try to use the QSqlRelationalTableModel combined with a combo box delegate. The combo box delegate shows a list of size units, picked from a sperate table in the db.

    It works this far:

    • the table is shown
    • the unit-indexes are resolved to the units
    • the combo box is shown on click with all available options from the db

    What doesn't work is writing the chosen entry from the combo box back to the DB because I haven't figured out how to get the unit-index from the DB to feed "model->setData" the correct value.

    Here is my setModelData in comboboxdelegate.cpp

    unit table example:

    vpe_index | unit
    1         |  kg
    2         |  g
    3         |  l
    4         |  ml
    5         |  pcs.
    ...
    
    void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
        if(QComboBox *cb = qobject_cast<QComboBox*>(editor)) {
    
            QString cbText = cb->currentText(); // text in combo box == text in db
            int cbIndex = cb->currentIndex(); // index in combo box
    
            int vpe_index =  ??? // index in db
    
            model->setData(index, vpe_index , Qt::EditRole);
        } else {
            QStyledItemDelegate::setModelData(editor, model, index);
        }
    }
    

    So I can get the current text from the combo box. But then? Do I have to execute another query on the db or is there a better way?

    in the delegate class I have access to

    QSqlQueryModel comboBoxModel
    

    which has the query "SELECT unit, vpe_index FROM vpe_typen" prepared and executed in my mainwindow.cpp.
    I'm sure there is a way to make use of this, but I don't know how.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      see_mountains
      wrote on 14 Jun 2019, 09:55 last edited by
      #2

      Ok, I have figured it out by myself. The key was looking at the comboBoxModel->data and getting the index from rows and coloumns. That's my solution:

      void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
          if(QComboBox *cb = qobject_cast<QComboBox*>(editor)) {
              QString cbText = cb->currentText();
              int vpe_index = -1;
      
              for (int row = 0; row < comboBoxModel->rowCount() ; row++) {
                  if (comboBoxModel->data(comboBoxModel->index(row, 0)) == cbText){
                      vpe_index = comboBoxModel->data(comboBoxModel->index(row, 1)).toInt();
                  }
              }
              if (vpe_index == -1) {
                  qDebug() << "Index not found";
              }
              else {
                  qDebug() << "Index set: " << model->setData(index, vpe_index, Qt::EditRole);
              }
      
          } else {
              QStyledItemDelegate::setModelData(editor, model, index);
          }
      }
      
      1 Reply Last reply
      0

      2/2

      14 Jun 2019, 09:55

      • Login

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