Important: Please read the Qt Code of Conduct -

[SOLVED] setItemDelegate does not seem to work

  • Hello,

    I am trying to set a delegate on a treeview, however the delegate does not seem to be set (or possibly is being over-written at some other place).

    In my window constructor I have the following

    @ ui->setupUi( this );
    ui->document->setModel( new DocumentModel( this ) );
    ui->document->setItemDelegateForColumn( 2, new ElementValueDelegate( this ) );@

    I set a break point in the delegate createEditor method and it never gets there. I looked at the properties in the UI file and there is no configuration for a delegate. I also tried setItemDelegate for the entire tableview, but still the same behaviour.

    The editTriggers property for the table view in the UI file is set to DoubleClicked and EditKeyPressed. Double clicking cells in the 3rd column, or selecting and hitting return have no effect.

    This is with Qt 4.8.1 on Mac OS X Snow Leopard. What could be wrong?


  • It's possible that the signature of your createEditor() does not exactly match the virtual function it is supposed to be replacing, e.g. forgot the const qualifier.

  • The methods in the delegate header are defined as follows:

    @ QWidget* createEditor( QWidget* parent,
    const QStyleOptionViewItem& option, const QModelIndex& index ) const;

        void setEditorData( QWidget* editor, const QModelIndex& index ) const;
        void setModelData( QWidget* editor, QAbstractItemModel* model,
            const QModelIndex& index ) const;@

    They seem exactly like the documentation shows. I have not over-ridden the paint method, since I do not think I require custom painting. Could that be the reason?

  • I implemented the paint method and just pass through to the QStyledItemDelegate::paint method. This method is indeed being called, but the others still are not firing. So, the delegate is being used, but I must be missing some other setting that would trigger invocation of the createEditor method.

  • If the delegate is set on the column (or view) and an editor is required then it should be called. The fact that the paint() method is being called is proof that the delegate has been attached.

    Your createEditor() is not being called. Are you getting the default editor, or no editor at all? Is the model returning flags() indicating that the column (or individual item) is editable?

    Are you attaching the delegate to the correct column? Column 2, i.e. the third column, of the model may not be the third visible column in the view if you are hiding or reordering columns.

  • Setting the ItemIsEditable flag for the column in the model was the missing step. Thanks for pointing it out.

Log in to reply