简体   繁体   English

如何根据它的内容调整QListWidgetItem的大小?

[英]How to resize QListWidgetItem according to it's content?

I'm adding a QListWidgetItems to a QListWidget. 我正在向QListWidget添加一个QListWidgetItems。 Is there any way to set the size of the QListWidgetItem according to it's content data? 有没有办法根据它的内容数据设置QListWidgetItem的大小?

QSize size(50, 20);
QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
newItem1->setSizeHint(size);
listWidget->addItem(newItem1); //listWidget is previously created
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
newItem2->setSizeHint(size);
listWidget->addItem(newItem2);

Text of newItem1 is displayed without any problem. 显示newItem1的文本没有任何问题。 But the newItem2 text is not fully displayed. 但是newItem2文本没有完全显示。 It only shows few characters and then "..." as text elide. 它只显示几个字符然后“...”作为文本elide。 How to show the complete text without the elide? 如何在没有遗嘱的情况下显示完整的文字? I want to set the size according to the size of item's data without setting any constant numbers. 我想根据项目数据的大小设置大小而不设置任何常数。

I simply copy/pasted your code, removed setSizeHint() function call for both items and added some of mine. 我只是复制/粘贴你的代码,删除了两个项目的setSizeHint()函数调用,并添加了一些我的。 Here's working code: 这是工作代码:

QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
ui->listWidget->addItem(newItem1); 
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
ui->listWidget->addItem(newItem2);
ui->listWidget->setFixedSize(ui->listWidget->sizeHintForColumn(0) + ui->listWidget->frameWidth() * 2,
                             ui->listWidget->sizeHintForRow(0) * ui->listWidget->count() + 2 * ui->listWidget->frameWidth());

在此输入图像描述

As you can see, both items are fully displayed. 如您所见,两个项目都完全显示。

Actually I have accidentally enabled uniformItemSizes for the QListWidget. 实际上我不小心为QListWidget启用了uniformItemSizes。 When I disable that, QListWidgetItems were automatically resized according to it's content. 当我禁用它时,QListWidgetItems会根据它的内容自动调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM