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?

    Ingo



  • 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();
    item->setSizeHint(QSize(0,60));
    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item,myItem);@

    but if you want use QListView, you should override the QItemDelegate's paint for a view:
    @if(index.column()==cbxColumnNumber)
    {
    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);
    
      QApplication::style()->drawControl(QStyle::CE_CheckBox,
                                         &check_box_style_option,
                                         painter);
    }
    else
    {
        QItemDelegate::paint(painter, option, index);
    }@
    

    for more information see:
    "http://doc.qt.digia.com/qt/qitemdelegate.html":http://doc.qt.digia.com/qt/qitemdelegate.html
    or
    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.