Important: Please read the Qt Code of Conduct -

Need help on QAbstractItemView/QAbstractItemDelegate.

  • Banned

    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

    Are you sure you need a radio button (only one can be selected at a time) or a check box? I'm asking because getting a checkbox is much easier and does not require any custom delegates. It all boils down to:

    • model::flags() should return Qt::ItemIsUserCheckable
    • model::data() should return Qt::CheckState::Checked or Qt::CheckState::Unchecked for Qt::ItemDataRole::CheckStateRole

    And of course, nothing stops you from manually adding code that will keep these options exclusive (like in radio button), and you can use QSS to provide custom icons which will mimick radio buttons. A bit hacky, but good solution :-)

    If you definitely want radio buttons, though, you can subclass QListView, then set a custom delegate using setItemDelegate(). It will take more work for sure.

  • Hi,
    It actually is much easier that you think but we need a few infos:

    • do you mean radio buttons or checkboxes (see QRadioButton vs QCheckBox to understand the difference)
    • what model are you using?

Log in to reply