QComboBox 是否可以显示与其列表中不同的值? [英] Can a QComboBox display a different value than whats in it's list?

查看:50
本文介绍了QComboBox 是否可以显示与其列表中不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux 上使用 Qt 5.9,我有一个带有多个标签的 QComboBox.

qc = 新的 QComboBox;qc->addItem(tr("Red"));qc->addItem(tr("Green"));qc->addItem(tr("Blue"));

假设用户激活了 QComboBox,下拉列表中显示了 3 个颜色标签.然后用户选择第一个项目(红色).

我想要做的是让 QComboBox 显示与所选值不同的值.即,如果选择红色,则显示一个数字,第一个项目可能是 1(或者它可能是红色的 R),如果选择绿色,则第二个项目显示 2(或 G).

我这样做的目标是使用比实际显示选择的完整文本所需的更少的显示空间(更窄),因为我的一些项目字符串很长并且当 QComboBox 是需要的更短的标签时在它的下拉状态未激活.此外,项目字符串是描述性的,缩写会更好地显示.


使用 Marek 的例子,认为这可能会有所帮助.这就是我所拥有的.我期待如果用户从列表中选择,那么应该在之后显示 R、G 或 B.

QStandardItem *red = new QStandardItem();red->setData(tr("Red"), Qt::DisplayRole);red->setData("R", Qt::UserRole);QStandardItem *green = new QStandardItem();绿色-> setData(tr("Green"), Qt::DisplayRole);绿色-> setData("G", Qt::UserRole);QStandardItem *blue = new QStandardItem();blue->setData(tr("Blue"), Qt::DisplayRole);blue->setData("B", Qt::UserRole);QStandardItemModel *rgb_model = new QStandardItemModel(this);rgb_model->setItem(0, red);rgb_model->setItem(1, green);rgb_model->setItem(2, blue);QComboBox *rgb_cb = 新的 QComboBox();rgb_cb->setModel(rgb_model);

我觉得这是因为我不太了解如何使用 Qt::UserRole.

解决方案

是的,这是可能的.QComboBox 使用数据模型来管理项目.您必须提供自己的数据模型,以及具有各自数据值的项目.

QStandardItem *itme1 = new QStandardItem();item1->setData(tr("Red"), Qt::DisplayRole);item1->setData("1", Qt::UserRole);//注意不一定是字符串.QStandardItem *itme2 = new QStandardItem();item2->setData(tr("Green"), Qt::DisplayRole);item2->setData("2", Qt::UserRole);QStandardItemModel *model = new QStandardItemModel(this);模式-> setItem(1, item1);模式-> setItem(2, item2);qc->setModel(模型);

它应该可以工作,但我没有测试它.至少这应该是一些线索.

请查看 QComboBox 文档,尤其是关于角色的内容.>

<小时>另一种解决方案是使用具有多种长度的翻译.您可以为单个字符串提供一对翻译.每个翻译应该比之前的翻译更短.

在这种情况下,QString 包含由空间字符分隔的所有可能性.当呈现这样的字符串时,将使用适合可用空间的第一个子字符串(在分隔符之间).

现在我不记得分隔符值是什么了.我很久以前使用过这个(使用 Qt 4.8),现在找不到它的参考.

Using Qt 5.9 on Linux, I have a QComboBox with several labels.

qc = new QComboBox;
qc->addItem(tr("Red"));
qc->addItem(tr("Green"));
qc->addItem(tr("Blue"));

Lets say a user activates the QComboBox and the 3 color labels are shown in the drop down list. The user then selects the 1st item (red).

What I want to do is have the QComboBox display a different value than what was selected. I.e., if red is selected, then a number is shown, possibly 1 for the first item (or it could be an R for Red), and if green is selected, then display 2 (or G) for the second item.

My goal in doing this is to use less display space (less wide) than is actually necessary to show the complete text of the selection because some of my item strings are quite long and a much shorter label is desired when the QComboBox is not activated in it's drop down state. Besides, the item strings are descriptive and abbreviations would work better for displaying.

Edit:
Using Marek's example, thought this might help. Here's what I have. I'm expecting if the user selects from the list, then an R, G, or a B should be displayed after.

QStandardItem *red = new QStandardItem();
red->setData(tr("Red"), Qt::DisplayRole);
red->setData("R", Qt::UserRole);

QStandardItem *green = new QStandardItem();
green->setData(tr("Green"), Qt::DisplayRole);
green->setData("G", Qt::UserRole);

QStandardItem *blue = new QStandardItem();
blue->setData(tr("Blue"), Qt::DisplayRole);
blue->setData("B", Qt::UserRole);

QStandardItemModel *rgb_model = new QStandardItemModel(this);
rgb_model->setItem(0, red);
rgb_model->setItem(1, green);
rgb_model->setItem(2, blue);

QComboBox *rgb_cb = new QComboBox();
rgb_cb->setModel(rgb_model);

I get the feeling it's because I don't quite understand how to use Qt::UserRole.

解决方案

Yes it is possible. QComboBox uses data model to manage items. You have to provide own data model, with items with respective data values.

QStandardItem *itme1 = new QStandardItem();
item1->setData(tr("Red"), Qt::DisplayRole);
item1->setData("1", Qt::UserRole); // note doesn't have to be a string.

QStandardItem *itme2 = new QStandardItem();
item2->setData(tr("Green"), Qt::DisplayRole);
item2->setData("2", Qt::UserRole);

QStandardItemModel *model = new QStandardItemModel(this);
mode->setItem(1, item1);
mode->setItem(2, item2);

qc->setModel(model);

It should work, but I didn't test it. At least this should be some clue.

Please review QComboBox documentation, especially about roles.


Another solution is use translations with multiple lengths. You can provide couple translation for a single string. Each translation should be graphically shorter than earlier one.

In such situation QString contains all possibilities separated by spatial character. When such string is rendered first substring (between separators) which will fit available space will be used.

Now I do not remember what is the separator value. I've used this very long time ago (with Qt 4.8) and now can't find reference for it.

这篇关于QComboBox 是否可以显示与其列表中不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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