Important: Please read the Qt Code of Conduct -

Delegate editor shows double text while editing

  • I have a table view, and I created my own QStyledItemDelegate.

    When I double click in a field that creates a text edit field, two sets of text are shown; the original and the new text that you are editing. The top portion of this image is how the Table looks before I double click in the cell to edit it. The bottom example shows the strange display.

    ! Text)!

    Note that this used to work, and now it has this strange behavior. Sadly, I don't use this particular dialog often, so i am uncertain when it began. It may have been introduced with QT 5.0, and it may be related to my move from Fedora 17 to Fedora 18.

    This is how I define my delegate

    @class LinkBackFilterDelegate : public QStyledItemDelegate
    explicit LinkBackFilterDelegate(QObject *parent = 0);

    /*! \brief Returns the widget used to edit the item specified by index for editing.

    • \param [in, out] parent Used to control how the editor widget appears.
    • \param [in] option Used to control how the editor widget appears.
    • \param [in] index Indicates which column is of interest.
    • \return Widget used to edit the specified data (so the editor is specific to the column).
      QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
      const QModelIndex &index) const;
    /*! \brief Set the data into the editor for the location based on the mode.
     *  The data is obtained from the model.
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &index) const;


    public slots:


    This is how I created my QLineEdit that causes the problem

    @QWidget *LinkBackFilterDelegate::createEditor(QWidget parent, const QStyleOptionViewItem &,
    const QModelIndex &index) const
    QVariant qvar = index.model()->data(index, Qt::EditRole);
    if (qvar.type() == QVariant::StringList)
    return new QComboBox(parent);
    else if (qvar.type() == QVariant::Bool)
    checkBox = new QCheckBox(parent);
    return checkBox;
    return new QLineEdit(parent);

    Any thoughts?

  • Side note, I originally used QItemDelegate, and have gone back to it since I was not styling anything!.

  • One final comment. I am able to avoid the problem using something like this, but it just feels wrong.

    @void LinkBackFilterDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    // This feels wrong, I should not need to do this, but, it works.
    // If I do not do this, then the underlying text box shows through to the
    // Editing text box.
    if ((current_state & (QStyle::State_Active | QStyle::State_HasFocus | QStyle::State_Selected)) == QStyle::State_Selected) {
    QItemDelegate::drawBackground( painter, option, index );
    } else {
    QItemDelegate::paint( painter, option, index );

  • No ideas? Well, here is the solution I finally settled on (since relying on the state will fail in some rare situations.... well, not that rare).

    @ //widget = new QLineEdit(parent);
    // Returns an expanding line editor.
    QWidget* widget = QStyledItemDelegate::createEditor(parent, option, index);

    // If I do not set this to true, then the "view" data shows through the text while editing.
    // Also, the expanding editor grows over existing controls, and you can see those through
    // the edit display as well (distracting).
    return widget;@

Log in to reply