c++qtdelegatesqtableviewmodel-view

Qt: start editing of cell after one click


By default the cell in QTableView starts being edited after double click. How to change this behavior. I need it to start editing after one click.

I have set combo-box delegate to the cell. When clicking the cell it only selects it. When double clicking on the cell the QComboBox editor is activated but not expanded. I want it to expand after just one click as if I added QComboBox by setCellWidget function of QTableWidget. I need the same effect by using model-view-delegate.


Solution

  • Edit after one click You can reimplement mousePressEvent in view you are using

    void YourView::mousePressEvent(QMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton) {
            QModelIndex index = indexAt(event->pos());
            if (index.column() == 0) { // column you want to use for one click
                edit(index);
            }
        }
        QTreeView::mousePressEvent(event);
    }
    

    Expanded QCombobox when edit You should imlement setEditorData in your subclass of QItemDelegate and at the end call showPopup.

    But it has some unexpected behaviour. QComboBox disappears when mouse leave its area. But for me it is advantage. I can select different item with single click and release.

    void IconDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        Q_UNUSED(index);
        QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
        // Add data
        comboBox->addItem(QIcon(":/icons/information16.png"), "info");
        comboBox->addItem(QIcon(":/icons/warning16.png"), "warning");
        comboBox->addItem(QIcon(":/icons/send16.png"), "send");
        comboBox->addItem(QIcon(":/icons/select16.png"), "select");
        comboBox->showPopup(); // <<<< Show popup here
    }
    

    Together it works fast way. Click and hold to choose item and commit data on release ( Just one click and release )

    If you want click to show expanded qcombobox and next click to choose/hide, I do not know solution for now.