QListWidgetItem与单选按钮 [英] QListWidgetItem with Radio Button

查看:308
本文介绍了QListWidgetItem与单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的第一个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


  1. 可以使用我用于checkBox的相同逻辑来创建单选按钮?

  2. 我使用了:

  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:

试图无图像演示

这是测试

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.


  1. 最灵活的一个:使用 QListView

  2. 继续使用经典的基于项目的界面( QListWidget ),并更改项目的窗口小部件通过子类 QListWidgetItem 或调用 QListWidgetItem :: setItemWidget

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

其中 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆