Donotalo
Donotalo

Reputation: 13025

Adding list of check boxes in a single widget in Qt

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.

Upvotes: 7

Views: 19259

Answers (3)

Luc Touraille
Luc Touraille

Reputation: 82031

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!

Upvotes: 13

Isira
Isira

Reputation: 489

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.

Upvotes: 13

Frank Osterfeld
Frank Osterfeld

Reputation: 25155

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.

Upvotes: 0

Related Questions