Question

I'm working on my first QT application and I have a problem with QListWidgetItems.

I will be having different kind of list. for checkboxed list using:

listElement[i]->setFlags(Qt::ItemIsEnabled);

listElement[i]->setCheckState(Qt::Unchecked);

works exactly as wanted.

But now I want a Radio Button list. so my question is in two parts

  1. can use the same logic that I used for checkBox to create Radio Buttons?
  2. I have used:

    listElement[i]->setFlags(Qt::ItemIsEnabled);
    
    QRadioButton *radio1 = new QRadioButton(0);
    
    dlList->setItemWidget(listElement[i],radio1);
    

this will display Items in the list with a radio Button, the problem is that the text is Over the Radio Button:

going to try to demonstrate without image

This is a test
o
for elements 1

instead for checkbox I have

   This is a test  

[]

   for element 1

how can I get the radioButton to align correctly with text?

New Questions:

Thanks alot for the answers my text is next to my RadioButton now.

Only thing there is no WordWrap, My text is Longer than maximum Size of the RadioButton. How can I get it to wordwrap:

rButton = new QRadioButton();

rButton->setFixedSize(LIST_TEXT_WIDTH_WO_ICON, LIST_TEXT_HEIGHT);

rButton->setStyleSheet("border:none");

rButton->setFont(segoe18Font);

rButton->setText("This is just a test for elementsss of type euh!!!");

rButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);

dropListWidget->setItemWidget(listElement, rButton);
Was it helpful?

Solution

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.

enter image description here

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top