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. QSqlRelationalTableModel Dopdown List for Field
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalTableModel Dopdown List for Field

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.0k Views 2 Watching
  • 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.
  • Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by Andy314
    #1

    Hello,
    I want in a qtableview (DamTV) a Dropdown-Listbox for a special column.

    QSqlRelationalTableModel Dams;
    Dams.setEditStrategy(QSqlTableModel::OnFieldChange);
    Dams.setTable(tblDamageTemplate);
    Dams.setRelation(4, QSqlRelation("qlkp_DamPart", "ID", "SCDesc"));
    ui->DamTV->setModel(&Dams);
    ui->DamTV->setItemDelegateForColumn(4, new QSqlRelationalDelegate(this));
    Dams.select();
    

    After select all is correct, I see instead of the primary key the lookup text in column 4.
    When I select a field and leave the field I see the primary key (ID).

    Why is that ? Why changes the editor the view representation of the data ?
    This should be independent when I understand the concept correctly.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,,

      Where are you using this code ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Andy314A 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,,

        Where are you using this code ?

        Andy314A Offline
        Andy314A Offline
        Andy314
        wrote on last edited by
        #3

        @SGaist
        In the constructor of the dialog after ui->setupUi(this);
        QSqlRelationalTableModel Dams; is of course in the header of the dialog.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Which OS and Qt version ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Andy314A 1 Reply Last reply
          0
          • SGaistS SGaist

            Which OS and Qt version ?

            Andy314A Offline
            Andy314A Offline
            Andy314
            wrote on last edited by Andy314
            #5

            Qt 5.6.0 32-bit, MSVC 2015, Windows 10 64-bit

            If found the code for the QSqlRelationalDelegate, have made a renamed copy of it for testing.

            class QSqlRelationalDelegate2: public QItemDelegate
            {
            public:
                explicit QSqlRelationalDelegate2(QObject *aParent = 0)
                    : QItemDelegate(aParent)  {}
                ~QSqlRelationalDelegate2()  {}
            
                QWidget *createEditor(QWidget *aParent,
                                      const QStyleOptionViewItem &option,
                                      const QModelIndex &index) const
                {
                    const QSqlRelationalTableModel *sqlModel
                            = qobject_cast<const QSqlRelationalTableModel *>(index.model());
                    QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                    if (!childModel)
                        return QItemDelegate::createEditor(aParent, option, index);
            
                    QComboBox *combo = new QComboBox(aParent);
                    combo->setModel(childModel);
                    combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(
                                            index.column()).displayColumn()));
                    combo->installEventFilter(const_cast<QSqlRelationalDelegate2 *>(this));
            
                    return combo;
                }
            
                void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                {
                    if (!index.isValid())
                        return;
            
                    QSqlRelationalTableModel *sqlModel = qobject_cast<QSqlRelationalTableModel *>(model);
                    QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                    QComboBox *combo = qobject_cast<QComboBox *>(editor);
                    if (!sqlModel || !childModel || !combo) {
                        QItemDelegate::setModelData(editor, model, index);
                        return;
                    }
            //        QSqlRelation newrel(sqlModel->relation(index.column()));
            
                    int currentItem = combo->currentIndex();
                    int childColIndex = childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn());
                    int childEditIndex = childModel->fieldIndex(sqlModel->relation(index.column()).indexColumn());
            
                    QString dd=childModel->data(childModel
                            ->index(currentItem, childColIndex), Qt::DisplayRole).toString();
            
                    sqlModel->setData(index,
                            childModel->data(childModel->index(currentItem, childColIndex),
                            Qt::DisplayRole), Qt::DisplayRole);
                    sqlModel->setData(index,
                            childModel->data(childModel->index(currentItem, childEditIndex),
                            Qt::EditRole), Qt::EditRole);
            //        sqlModel->setRelation(index.column(), newrel);
            //        sqlModel->dataChanged(index, index,
            //                QVector<int>()<<Qt::DisplayRole<<Qt::EditRole);
                }
                void paint(QPainter* painter,   const QStyleOptionViewItem& opt,
                           const QModelIndex& index) const
                {
                    QString s=index.data(Qt::DisplayRole).toString();
                    QStyleOptionViewItem so=opt;
                    drawDisplay(painter, so, so.rect, s);
                    return;
                }
            };
            

            Not even the overwriting of the paint function help. Should not have the delegate the control for the painting always ?

            In the setModelData function the last command

                    sqlModel->setData(index,
                            childModel->data(childModel->index(currentItem, childEditIndex),
                            Qt::EditRole), Qt::EditRole);
            

            makes the problem. After write back the data from the widget to the model I get the view shows the foreign key and not its lookup.

            Andy314A 1 Reply Last reply
            0
            • Andy314A Andy314

              Qt 5.6.0 32-bit, MSVC 2015, Windows 10 64-bit

              If found the code for the QSqlRelationalDelegate, have made a renamed copy of it for testing.

              class QSqlRelationalDelegate2: public QItemDelegate
              {
              public:
                  explicit QSqlRelationalDelegate2(QObject *aParent = 0)
                      : QItemDelegate(aParent)  {}
                  ~QSqlRelationalDelegate2()  {}
              
                  QWidget *createEditor(QWidget *aParent,
                                        const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const
                  {
                      const QSqlRelationalTableModel *sqlModel
                              = qobject_cast<const QSqlRelationalTableModel *>(index.model());
                      QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                      if (!childModel)
                          return QItemDelegate::createEditor(aParent, option, index);
              
                      QComboBox *combo = new QComboBox(aParent);
                      combo->setModel(childModel);
                      combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(
                                              index.column()).displayColumn()));
                      combo->installEventFilter(const_cast<QSqlRelationalDelegate2 *>(this));
              
                      return combo;
                  }
              
                  void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                  {
                      if (!index.isValid())
                          return;
              
                      QSqlRelationalTableModel *sqlModel = qobject_cast<QSqlRelationalTableModel *>(model);
                      QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
                      QComboBox *combo = qobject_cast<QComboBox *>(editor);
                      if (!sqlModel || !childModel || !combo) {
                          QItemDelegate::setModelData(editor, model, index);
                          return;
                      }
              //        QSqlRelation newrel(sqlModel->relation(index.column()));
              
                      int currentItem = combo->currentIndex();
                      int childColIndex = childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn());
                      int childEditIndex = childModel->fieldIndex(sqlModel->relation(index.column()).indexColumn());
              
                      QString dd=childModel->data(childModel
                              ->index(currentItem, childColIndex), Qt::DisplayRole).toString();
              
                      sqlModel->setData(index,
                              childModel->data(childModel->index(currentItem, childColIndex),
                              Qt::DisplayRole), Qt::DisplayRole);
                      sqlModel->setData(index,
                              childModel->data(childModel->index(currentItem, childEditIndex),
                              Qt::EditRole), Qt::EditRole);
              //        sqlModel->setRelation(index.column(), newrel);
              //        sqlModel->dataChanged(index, index,
              //                QVector<int>()<<Qt::DisplayRole<<Qt::EditRole);
                  }
                  void paint(QPainter* painter,   const QStyleOptionViewItem& opt,
                             const QModelIndex& index) const
                  {
                      QString s=index.data(Qt::DisplayRole).toString();
                      QStyleOptionViewItem so=opt;
                      drawDisplay(painter, so, so.rect, s);
                      return;
                  }
              };
              

              Not even the overwriting of the paint function help. Should not have the delegate the control for the painting always ?

              In the setModelData function the last command

                      sqlModel->setData(index,
                              childModel->data(childModel->index(currentItem, childEditIndex),
                              Qt::EditRole), Qt::EditRole);
              

              makes the problem. After write back the data from the widget to the model I get the view shows the foreign key and not its lookup.

              Andy314A Offline
              Andy314A Offline
              Andy314
              wrote on last edited by Andy314
              #6

              I found the delegate has indeed ever the control for the painting. Before editing all works.
              The problem is that after setModelData both the Display- and Editrole give always the foreign key for the changed cell.

              QString s1=index.data(Qt::DisplayRole).toString();
              QString s2=index.data(Qt::EditRole).toString();

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Can you provide a minimal compilable example that reproduce that behaviour ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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