QComboBox - 根据项目的数据设置所选项目

发布于 2024-10-05 13:05:34 字数 231 浏览 0 评论 0原文

从基于 enum 的唯一值的预定义列表中选择 QT 组合框中的项目的最佳方法是什么。

在过去,我已经习惯了 .NET 的选择风格,可以通过将 selected 属性设置为您想要选择的项目的值来选择项目:

cboExample.SelectedValue = 2;

是否有办法根据项目的数据使用 QT 来执行此操作,如果数据是C++枚举吗?

What would be the best way of selecting an item in a QT combo box out of a predefined list of enum based unique values.

In the past I have become accustomed to .NET's style of selection where the item can be selected by setting the selected property to the item's value you wish to select:

cboExample.SelectedValue = 2;

Is there anyway to do this with QT based on the item's data, if the data is a C++ enumeration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2024-10-12 13:05:34

您使用 findData() 查找数据值,然后使用 setCurrentIndex()

QComboBox* combo = new QComboBox;
combo->addItem("100",100.0);    // 2nd parameter can be any Qt type
combo->addItem .....

float value=100.0;
int index = combo->findData(value);
if ( index != -1 ) { // -1 for not found
   combo->setCurrentIndex(index);
}

You lookup the value of the data with findData() and then use setCurrentIndex()

QComboBox* combo = new QComboBox;
combo->addItem("100",100.0);    // 2nd parameter can be any Qt type
combo->addItem .....

float value=100.0;
int index = combo->findData(value);
if ( index != -1 ) { // -1 for not found
   combo->setCurrentIndex(index);
}
热鲨 2024-10-12 13:05:34

您还可以查看 QComboBox 中的 findText(const QString & text) 方法;它返回包含给定文本的元素的索引(如果未找到则为 -1)。
使用这种方法的好处是,添加项目时不需要设置第二个参数。

这是一个小例子:

/* Create the comboBox */
QComboBox   *_comboBox = new QComboBox;

/* Create the ComboBox elements list (here we use QString) */
QList<QString> stringsList;
stringsList.append("Text1");
stringsList.append("Text3");
stringsList.append("Text4");
stringsList.append("Text2");
stringsList.append("Text5");

/* Populate the comboBox */
_comboBox->addItems(stringsList);

/* Create the label */
QLabel *label = new QLabel;

/* Search for "Text2" text */
int index = _comboBox->findText("Text2");
if( index == -1 )
    label->setText("Text2 not found !");
else
    label->setText(QString("Text2's index is ")
                   .append(QString::number(_comboBox->findText("Text2"))));

/* setup layout */
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(_comboBox);
layout->addWidget(label);

You can also have a look at the method findText(const QString & text) from QComboBox; it returns the index of the element which contains the given text, (-1 if not found).
The advantage of using this method is that you don't need to set the second parameter when you add an item.

Here is a little example :

/* Create the comboBox */
QComboBox   *_comboBox = new QComboBox;

/* Create the ComboBox elements list (here we use QString) */
QList<QString> stringsList;
stringsList.append("Text1");
stringsList.append("Text3");
stringsList.append("Text4");
stringsList.append("Text2");
stringsList.append("Text5");

/* Populate the comboBox */
_comboBox->addItems(stringsList);

/* Create the label */
QLabel *label = new QLabel;

/* Search for "Text2" text */
int index = _comboBox->findText("Text2");
if( index == -1 )
    label->setText("Text2 not found !");
else
    label->setText(QString("Text2's index is ")
                   .append(QString::number(_comboBox->findText("Text2"))));

/* setup layout */
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(_comboBox);
layout->addWidget(label);
独自←快乐 2024-10-12 13:05:34

如果您知道组合框中要选择的文本,只需使用 setCurrentText() 方法来选择该项目。

ui->comboBox->setCurrentText("choice 2");

来自 Qt 5.7 文档

如果组合是 setCurrentText(),则设置器 setCurrentText() 只需调用 setEditText()
框是可编辑的。否则,如果列表中有匹配的文本,
currentIndex 设置为相应的索引。

因此,只要组合框不可编辑,函数调用中指定的文本就会在组合框中被选中。

参考: http://doc.qt.io/qt-5/qcombobox .html#currentText-prop

If you know the text in the combo box that you want to select, just use the setCurrentText() method to select that item.

ui->comboBox->setCurrentText("choice 2");

From the Qt 5.7 documentation

The setter setCurrentText() simply calls setEditText() if the combo
box is editable. Otherwise, if there is a matching text in the list,
currentIndex is set to the corresponding index.

So as long as the combo box is not editable, the text specified in the function call will be selected in the combo box.

Reference: http://doc.qt.io/qt-5/qcombobox.html#currentText-prop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文