C++ QComboBox - 根据项目的数据设置选定的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4340415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:10:23  来源:igfitidea点击:

QComboBox - set selected item based on the item's data

c++qtuser-interfacecomboboxqcombobox

提问by cweston

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

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

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:

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

cboExample.SelectedValue = 2;

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

如果数据是 C++ 枚举,是否可以根据项目的数据使用 QT 执行此操作?

回答by Martin Beckett

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

您查找数据的值,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);
}

回答by Alo?ké Go

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.

您还可以查看 QComboBox 中的 findText(const QString & text) 方法;它返回包含给定文本的元素的索引(-1 如果未找到)。使用这种方法的好处是添加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);

回答by DowntownDev

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

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

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

From the Qt 5.7 documentation

来自 Qt 5.7 文档

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.

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

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

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