QStyledItemDelegate with editor-dialog

  • Hello,
    I want to open an editor-dialog when I activate the editing in a QTableView-cell. Therefore I created a ItemDelegate:

    class TMemoEditDelegate : public QStyledItemDelegate
        TMemoEditDelegate(QObject* parent):QStyledItemDelegate(parent) {}
        bool editorEvent(QEvent *event, QAbstractItemModel *model,
                                       const QStyleOptionViewItem &option,
                                       const QModelIndex &index)
            //InfoDlg("edit it");
            TTextEditForm d;
            d.Text = index.data().toString();
            bool ok = d.execBool();
                model->setData(index, d.Text);
            return QStyledItemDelegate::editorEvent(event,model,option,index);

    The editing works, but the problem is the edit-trigger. In the QTableView I have set only the "DoubleClicked" edit trigger.
    But a simple click on a unselected row in the specific column opens the dialog.
    Therefore no simple selection is possible. In the other columns the edit-trigger works correctly.
    Whats the problem ?

  • Moderators

    The short (and not so correct) answer is - check the type of the event. You should only open the dialog if event->type() == QEvent::MouseButtonDblClick. Right now you're responding to all of them - press, release, dblclick

    The longer answer is that you're entirely bypassing the editor mechanism so the edit trigger is completely ignored. The proper way to do it is to implement createEditor, not editorEvent. This way figuring out if the editor should be shown is done by Qt according to the edit trigger setting. Also instead of manually calling data() and setData() you should implement setEditorData and setModelData.

  • Hi @Chris-Kawa,
    the event-filtering works. Maybe a good way to implement other edit-triggers (F12 for example).
    Nevertheless I will try the proper way. In the createEditor I can generate a dialog and give this back as a QWidget. Then this widget is under control of the QTableView. If I click in an other cell the editor closes and I read in setModelData the data from the dialog before.
    So, what can I do if the dialog must have a Ok and Cancel button to accept or discard the data ?
    (In my current version I use even a modal dialog.)

  • Moderators

    If I click in an other cell the editor closes

    If the dialog is modal you shouldn't be able to click on anything else when it's open.
    This works as expected (modal dialog) for me:

    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const override
        QDialog* d = new QDialog(parent);
        return d;

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.