QListWidgetItem与单选按钮 [英] QListWidgetItem with Radio Button
问题描述
我正在处理我的第一个QT应用程序,我有一个问题与 QListWidgetItems
。
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);
完全按照需要工作。
现在我想要一个单选按钮列表。所以我的问题分为两部分
But now I want a Radio Button list. so my question is in two parts
- 可以使用我用于checkBox的相同逻辑来创建单选按钮?
-
我使用了:
- can use the same logic that I used for checkBox to create Radio Buttons?
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:
试图无图像演示
这是测试
o
元素1
This is a test
o
for elements 1
而不是复选框我有
This is a test
[]
for element 1
如何让radioButton与文本正确对齐?
how can I get the radioButton to align correctly with text?
新问题:
感谢您的答案我的RadioButton现在。
Thanks alot for the answers my text is next to my RadioButton now.
只有没有WordWrap的东西,我的文本比RadioButton的最大尺寸大。我如何得到它wordwrap:
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);
推荐答案
正如你可能已经阅读,
As you may have read, there are two approaches to achieve what you want.
- 最灵活的一个:使用
QListView
- 继续使用经典的基于项目的界面(
QListWidget
),并更改项目的窗口小部件通过子类QListWidgetItem
或调用QListWidgetItem :: setItemWidget
。
- The most flexible one: use a
QListView
, implement a new delegate and a model if necessary. - Keep using the classic item-based interface (
QListWidget
) and change the item's widgets either by sub-classingQListWidgetItem
or callingQListWidgetItem::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")));
其中 ui-> listWidget
指向保存项目的 QListWidget
的指针。
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.
这篇关于QListWidgetItem与单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!