QListWidgetItem with Radio Button

半世苍凉 提交于 2019-12-05 19:15:47

As you may have read, there are two approaches to achieve what you want.

  1. The most flexible one: use a QListView, implement a new delegate and a model if necessary.
  2. Keep using the classic item-based interface (QListWidget) and change the item's widgets either by sub-classing QListWidgetItem or calling QListWidgetItem::setItemWidget.

Since the question points towards the second one, I'll try to provide the simplest item-based solution.

The following piece of code generates the list widget in the picture.

QListWidgetItem *it;

it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item 1")));

it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item 2")));

// .
// .
// .

it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item N")));

where ui->listWidget is a pointer to the QListWidget that holds the items.

I hope this helps. As far as I understand, that's what you need.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!