Qt QListWidgetItem 多行
我有一个非常简单的 QListWidget 对象,我想构建一个文件夹列表。 当我将一个项目添加到我的列表中时,我要做的就是:
void LessCC::on_addFolderButton_clicked()
{
QString dirName = QFileDialog::getExistingDirectory(this, tr("Choose Directory"), QDir::homePath(), QFileDialog::ShowDirsOnly);
QListWidgetItem* newItem = new QListWidgetItem(QIcon(":/resources/icons/folder.png"), dirName, 0, 0);
this->ui->folderListWidget->addItem(newItem);
}
这是有效的,但我希望我的项目具有多行或多列信息(具有不同的样式)。
我听说过 QStyledItemDelegate 但是我真的不明白它是如何工作的,因为我发现的所有其他解决方案对于这样的简单(?)< /em> 的事情。
这是唯一解决方案,还是有一些我没有看到的更简单的解决方案?
希望有人能帮助我。
I have a really simple QListWidget
object and I want to build a folder list.
When I add an item to my list here is what I do :
void LessCC::on_addFolderButton_clicked()
{
QString dirName = QFileDialog::getExistingDirectory(this, tr("Choose Directory"), QDir::homePath(), QFileDialog::ShowDirsOnly);
QListWidgetItem* newItem = new QListWidgetItem(QIcon(":/resources/icons/folder.png"), dirName, 0, 0);
this->ui->folderListWidget->addItem(newItem);
}
This is working but I want my items to have multiple lines or column of informations (with different style).
I heard about the QStyledItemDelegate but I don't really understand how it works because all other solutions I've found seems really complicated for such simple(?) thing.
Is this the only solution or maybe there is something much more simple I did not see ?
Hope someone can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在没有确切了解您希望列表项是什么样子的情况下,建议“最佳”解决方案有点困难,但我们会尝试一下。
实际上,Qt 线程这里上有一篇很棒的文章,最后最后给出了他们用来创建列表的所有代码,类似于他们在顶部显示的列表。
基本上,每个项目都有一个大图标,右侧有标题和描述文本,每个项目的样式都不同。
创建自定义委托听起来很可怕,但它基本上只是为列表提供一个自定义小部件。它的工作原理本质上就像一个模板。您可以定义列表项的外观,并使用列表中的数据来绘制它。您只需继承 QAbstractItemDelegate 即可。
最大的工作是编写绘制函数的代码。我将在这里展示基础知识,但您可以参考上面的链接以获取更长的示例。
所以你可以在这段代码中看到我们从列表中获取了三位数据。图标、标题和说明。然后我们显示绘制图标,并按照我们喜欢的方式绘制两个文本字符串。但重要的部分是获取数据本身。我们基本上是要求列表使用传递给我们的“索引”为我们提供数据。
您会注意到每一位信息都是使用不同的“角色”检索的。通常,列表只显示一项内容 - 通过 DisplayRole 访问。图标存储在 DecorationRole 中。但如果您想存储更多内容,那么您可以开始使用 UserRole。您可以使用 UserRole、UserRole +1、UserRole +2 等来存储大量内容。
那么如何在每个项目中存储所有这些信息。简单...
最后,如何使用您精美的新委托使列表显示项目?
希望这能澄清一些事情。
It's a little difficult to suggest the "best" solution without seeing exactly what you want your list item to look like, but we'll give it a try.
There is actually a great post on the Qt thread here that finally at the end gives all the code they use to create a list sort of like the one they show at the top.
Basically, each item has a large icon, and to the right there is a title and description text, each styled differently.
Creating a custom delegate sounds scary, but it is basically just providing a custom widget for the list. It works essentially like a template. You define what you want your list item to look like, and you paint it using data from list. You just can inherit from QAbstractItemDelegate.
The biggest job is to code the paint function. I'll just show the basics here, but you can reference the link above for a longer example.
So you can see in this code we are getting three bits of data from the list. The icon, the title, and the description. Then we are displaying drawing the icon, and drawing the two text strings how we like. But the important part is getting the data itself. We are basically asking the list to give us the data using the "index" that was passed to us.
You'll notice that each bit of information was retrieved using a different "role". Normally a list has only one thing it displays - which is accessed through the DisplayRole. Icons are stored in the DecorationRole. But if you want to store more stuff, then you start using UserRole. You can store a whole slew of things using UserRole, UserRole +1, UserRole +2 etc....
So how you do you store all this information in each item. Easy...
And finally, how do you make the list display the item using your fancy new delegate?
Hopefully that clarified things a little bit.