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. reach QComboBox (QStyledItemDelegate delegate) in QTableView from outside
Qt 6.11 is out! See what's new in the release blog

reach QComboBox (QStyledItemDelegate delegate) in QTableView from outside

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 946 Views 1 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.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by
    #1

    Hi guys!
    I have a QtableView with QComboBox delegate, and a little backgroundcolor delegate (with QSqlRelationalTableModel). I have to reach the QComboBox, because i have to set current text on it.
    How can i do that?
    Because if I add a new record, i see this:2021-10-06_21h17_56.png
    (the green background is my color delegate)
    I tried it with this:

    ui->tableView->closePersistentEditor(model->index(getSelectedRow(), 1));
    

    but the solution is this:
    2021-10-06_21h14_03.png
    Now my QCombobox is not empty its okay. But lose the green background and always triggered ( always see the arrow and border etc..).
    And i tried it this code:

    const QModelIndex idx = model->index(getSelectedRow(), 1);
                QWidget* wid = ui->tableView->indexWidget(idx);
                QComboBox* box = qobject_cast<QComboBox*>(wid);
    
                if (box)
                {
                    qDebug()<<"catch it";
                }
    

    But nothing happened, dont get the QComboBox.
    So i have no idea how can i achieve it. :( Any idea?
    Thank you guys! (and sorry if I lame again)

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

      Hi,

      You are talking about two delegates. How did you implement them ?
      How are you using them ?

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

      KaguroK 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        You are talking about two delegates. How did you implement them ?
        How are you using them ?

        KaguroK Offline
        KaguroK Offline
        Kaguro
        wrote on last edited by
        #3

        @SGaist
        This is where i create the table:

        void ter::setTableModel(const QString& dataBase, const QString& table)
        {    
            this->model = new QSqlRelationalTableModel(this, QSqlDatabase::database(dataBase));
            this->model->setTable(table);
            this->model->setEditStrategy(QSqlTableModel::OnManualSubmit);
            this->model->select();
            this->ui->tableView->setModel(model); 
        
            connect(this->model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(preValidCell(const QModelIndex&, const QModelIndex& )));
        
            this->ui->tableView->setSortingEnabled(true);
            this->ui->tableView->sortByColumn(0,Qt::AscendingOrder);    
        
            this->ui->tableView->horizontalHeader()->setSectionsMovable(true);
            this->ui->tableView->horizontalHeader()->setDragDropOverwriteMode(true);
            this->ui->tableView->horizontalHeader()->setDragEnabled(true);
            this->ui->tableView->horizontalHeader()->setDragDropMode(QAbstractItemView::InternalMove);
            this->ui->tableView->resizeColumnsToContents();    
        
            connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QFXTorzs::slotLoadTransaction);
        }
        

        When I create te relation:

        void ter::setComboCells(QString columnName, const QString &strTableName, const QString &strKey, const QString &strField)
        {
            this->model->setRelation(model->fieldIndex(strForeignKey), QSqlRelation(strTableName, strKey, strField);
            this->model->select();    
            ui->tableView->setModel(model);
            ui->tableView->setItemDelegate(new ColorDelegate(Qt::color0,ui->tableView));
        }
        

        This is my ColorDelegate:

        class ColorDelegate: public QSqlRelationalDelegate
        {
        public:
            QColor color;
            ColorDelegate(QColor colo, QObject *parent = nullptr) : QSqlRelationalDelegate(parent)
            {
                color = colo;
            }
        
        public:
            virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                               const QModelIndex &index) const
            {
                if(color!=Qt::color0)
                {
                    drawBackground(painter, option, index);
                }
        
                QStyleOptionViewItem itemOption(option);
                if (itemOption.state & QStyle::State_HasFocus)
                    itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
        
                QSqlRelationalDelegate::paint(painter, itemOption, index);
            }
        private:
            bool shouldBeBold(const QModelIndex &){
                return true;
            }
        
        protected:
            virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const
            {
                Q_UNUSED(index);
                painter->fillRect(option.rect, color);
            }
        };
        

        When i hit the new record creater button:

        void ter::on_toolButtonNew_clicked()
        {
                this->model->insertRow(getSelectedRow()+1);
                QModelIndex index = ui->tableView->model()->index(getSelectedRow()+1, ui->tableView->columnAt(0));
                ui->tableView->setCurrentIndex(index);
                ui->tableView->edit(index);
                ui->tableView->setItemDelegateForRow(getSelectedRow(),new ColorDelegate(Qt::green,ui->tableView));
        }
        

        Sorry , my memory was wrong. Not two delegete just one with different parameter.

        SGaistS 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Your delegate never actually deals with the combobox and from your original post I suspect you are inexlicably using the dreaded setIndexWidget. Could you confirm my suspicions?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          KaguroK 1 Reply Last reply
          0
          • VRoninV VRonin

            Your delegate never actually deals with the combobox and from your original post I suspect you are inexlicably using the dreaded setIndexWidget. Could you confirm my suspicions?

            KaguroK Offline
            KaguroK Offline
            Kaguro
            wrote on last edited by Kaguro
            #5

            @VRonin
            I think yes you are right.
            ComboBox appaear because the model is QSqlRelationalTableModel and i linked a realtion with setRelation function.
            So any advice?

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

              Do you mean you can't dismiss the editor after you did your choice ?

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

              KaguroK 1 Reply Last reply
              0
              • SGaistS SGaist

                Do you mean you can't dismiss the editor after you did your choice ?

                KaguroK Offline
                KaguroK Offline
                Kaguro
                wrote on last edited by
                #7

                @SGaist
                I mean the editor is still active, I can dismiss but u can see in the last picture the editor is still active, not as same as the other cells in that column.
                So I would like to achive that when i add a new record, the first cell jump to edit (its done) and my editor select the first element turn back like the others but with green background.

                1 Reply Last reply
                0
                • KaguroK Kaguro

                  @SGaist
                  This is where i create the table:

                  void ter::setTableModel(const QString& dataBase, const QString& table)
                  {    
                      this->model = new QSqlRelationalTableModel(this, QSqlDatabase::database(dataBase));
                      this->model->setTable(table);
                      this->model->setEditStrategy(QSqlTableModel::OnManualSubmit);
                      this->model->select();
                      this->ui->tableView->setModel(model); 
                  
                      connect(this->model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(preValidCell(const QModelIndex&, const QModelIndex& )));
                  
                      this->ui->tableView->setSortingEnabled(true);
                      this->ui->tableView->sortByColumn(0,Qt::AscendingOrder);    
                  
                      this->ui->tableView->horizontalHeader()->setSectionsMovable(true);
                      this->ui->tableView->horizontalHeader()->setDragDropOverwriteMode(true);
                      this->ui->tableView->horizontalHeader()->setDragEnabled(true);
                      this->ui->tableView->horizontalHeader()->setDragDropMode(QAbstractItemView::InternalMove);
                      this->ui->tableView->resizeColumnsToContents();    
                  
                      connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QFXTorzs::slotLoadTransaction);
                  }
                  

                  When I create te relation:

                  void ter::setComboCells(QString columnName, const QString &strTableName, const QString &strKey, const QString &strField)
                  {
                      this->model->setRelation(model->fieldIndex(strForeignKey), QSqlRelation(strTableName, strKey, strField);
                      this->model->select();    
                      ui->tableView->setModel(model);
                      ui->tableView->setItemDelegate(new ColorDelegate(Qt::color0,ui->tableView));
                  }
                  

                  This is my ColorDelegate:

                  class ColorDelegate: public QSqlRelationalDelegate
                  {
                  public:
                      QColor color;
                      ColorDelegate(QColor colo, QObject *parent = nullptr) : QSqlRelationalDelegate(parent)
                      {
                          color = colo;
                      }
                  
                  public:
                      virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                                         const QModelIndex &index) const
                      {
                          if(color!=Qt::color0)
                          {
                              drawBackground(painter, option, index);
                          }
                  
                          QStyleOptionViewItem itemOption(option);
                          if (itemOption.state & QStyle::State_HasFocus)
                              itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
                  
                          QSqlRelationalDelegate::paint(painter, itemOption, index);
                      }
                  private:
                      bool shouldBeBold(const QModelIndex &){
                          return true;
                      }
                  
                  protected:
                      virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option,
                                                  const QModelIndex &index) const
                      {
                          Q_UNUSED(index);
                          painter->fillRect(option.rect, color);
                      }
                  };
                  

                  When i hit the new record creater button:

                  void ter::on_toolButtonNew_clicked()
                  {
                          this->model->insertRow(getSelectedRow()+1);
                          QModelIndex index = ui->tableView->model()->index(getSelectedRow()+1, ui->tableView->columnAt(0));
                          ui->tableView->setCurrentIndex(index);
                          ui->tableView->edit(index);
                          ui->tableView->setItemDelegateForRow(getSelectedRow(),new ColorDelegate(Qt::green,ui->tableView));
                  }
                  

                  Sorry , my memory was wrong. Not two delegete just one with different parameter.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  I missed that part:

                  @Kaguro said in reach QComboBox (QStyledItemDelegate delegate) in QTableView from outside:

                  ui->tableView->edit(index);
                  ui->tableView->setItemDelegateForRow(getSelectedRow(),new ColorDelegate(Qt::green,ui->tableView));

                  I think you have an issue there, you are first starting the édition and then applying a delegate. Why do you set it at that time ?

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

                  KaguroK 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    I missed that part:

                    @Kaguro said in reach QComboBox (QStyledItemDelegate delegate) in QTableView from outside:

                    ui->tableView->edit(index);
                    ui->tableView->setItemDelegateForRow(getSelectedRow(),new ColorDelegate(Qt::green,ui->tableView));

                    I think you have an issue there, you are first starting the édition and then applying a delegate. Why do you set it at that time ?

                    KaguroK Offline
                    KaguroK Offline
                    Kaguro
                    wrote on last edited by
                    #9

                    @SGaist Sorry for super late, i have so many other task in my job nowadays. I will aswer it tomorrow as i can! Thanks your help!:)

                    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