7

I'm using Qt Creator 2.0.1 (Qt 4.7). I need a widget which can hold multiple check boxes vertically. The check boxes will be added to the widget dynamically upon user interaction with other UI element. The widget will have fixed width and height so if there are too many check boxes, a vertical scroll bar should appear.

What I want is, imagine a QListWidget, where the list items can be check boxes.

Which widget will allow me to do that?

Thanks.

3 Answers 3

13

Well you could indeed use a QListWidget with checkable items (see void QListWidgetItem::setFlags, or use a QScrollArea containing a QWidget where you would dynamically add QCheckBoxes. The choice is up to you!

Sign up to request clarification or add additional context in comments.

Comments

13

Use QListWidget for the area.

QStringList  itemLabels= getLabels();

QStringListIterator it(itemLabels);
while (it.hasNext())
{
      QListWidgetItem *listItem = new QListWidgetItem(it.next(),listWidget);
      listItem->setCheckState(Qt::Unchecked);
      ui->listWidget->addItem(listItem);
}

this will automatically make all the elements checkable and when the list increases it will enable scrolling.

1 Comment

Moreover, if you construct QListWidgetItem with the list widget as its parent widget, it is not necessary to add the item to the list.
0

An alternative to QListWidget is a QScrollArea with a widget inside, which has a QVBoxLayout. To that layout, you can add QCheckboxes dynamically. You must call updateGeometry() after adding a new widget, otherwise the UI might not update if already visible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.