QListWidgetItem with Radio Button

让人想犯罪 __ 提交于 2019-12-07 14:26:44

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/18904155/qlistwidgetitem-with-radio-button

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