Important: Please read the Qt Code of Conduct -

QListBox(View) with checkbox sample?

  • Hi,
    I tried to find a sample or description how to use a QListBox(View) with a checkbox in an very easy mode.
    I just want to show a list with option that have a checkbox at the beginning of the text. BUt I only find Q3.. things,
    nothing new. Anybody knows where I can find a good and easy sample?


  • Hi pixbyte!
    In case if you use QListWidget, you can just set the widget for an item:
    @QCheckBox *myItem = new QCheckBox(this);
    //settings data for item
    QListWidgetItem *item = new QListWidgetItem();

    but if you want use QListView, you should override the QItemDelegate's paint for a view:
    bool checked = index.model()->data(index, Qt::DisplayRole).toBool();

      QStyleOptionButton check_box_style_option;
      check_box_style_option.state |= QStyle::State_Enabled;
      if (checked) {
        check_box_style_option.state |= QStyle::State_On;
      } else {
        check_box_style_option.state |= QStyle::State_Off;
      check_box_style_option.rect = CheckBoxRect(option);
        QItemDelegate::paint(painter, option, index);

    for more information see:
    use QStandardItemModel as a model and make items checkable with QStandardItem::setCheckable ( bool checkable ) method.

  • Using QListView and QStandardItemModel is the easiest option

Log in to reply