Important: Please read the Qt Code of Conduct -

Need help on QAbstractItemView/QAbstractItemDelegate.

  • Hi, I'm new to programming and Qt, pardon me if I'm asking anything stupid. I have a doubt regarding QListView. I have a populated model with 3 keys, I need to add Radio Buttons to each index. I initially used QListView which couldn't help me achieve that. I realized I needed a delegate to do that. QTreeView usually uses QStyledItemDelegate so what should I do to have a list view with radio buttons. I read on Qt forum which said I need to use QAbstractItemView, so I was wondering if I need to QAbstractItemView instead of QListView or subclass QAbstractItemDelegate and then use QAbstractItemView? If I'm subclassing which function do I need to use.

  • Moderators

    generally the cells are just painted in the delegate.
    there is setIndexWidget() method on the QListView.
    Or if checkboxes are also ok you can set the items in the model checkable and the default delegate will paint checkboxes beside the text

  • Lifetime Qt Champion

    Normally you would use QStyledItemDelegate
    Like seen here for a CheckBox delegate.

    You need to implement the following functions.

    class BooleanCheckBoxDelegate : public QStyledItemDelegate
        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
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

    Do note that a checkbox delegate is not needed as it can do it by itself but to use for RadioButton,
    only requires changing QCheckBox used to QRadioButton.

    But, radio buttons is exclusive (one can be selected ) and if you need that you have to check the other
    rows in setModelData and act accordingly.

Log in to reply