Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

reach QComboBox (QStyledItemDelegate delegate) in QTableView from outside



  • 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)


  • Lifetime Qt Champion

    Hi,

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



  • @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.



  • 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?



  • @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?


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

    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 ?


Log in to reply