Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListBox(View) with checkbox sample?
Forum Updated to NodeBB v4.3 + New Features

QListBox(View) with checkbox sample?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pixbyte
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        Using QListView and QStandardItemModel is the easiest option

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved