在Qstringlist中找到对应的Qstring
时间: 2024-03-08 19:49:17 浏览: 18
可以使用QStringList的indexOf函数来找到对应的QString在QStringList中的索引,然后再使用QStringList的value函数获取对应的QString。示例代码如下:
```c++
QStringList list;
list << "apple" << "banana" << "orange";
int index = list.indexOf("banana");
QString str = list.value(index);
```
上述代码中,indexOf函数返回的是对应QString在QStringList中的索引,即1;然后使用value函数获取索引为1的QString,即"banana"。
相关问题
QMap<QString, QStringList>
QMap<QString, QStringList> 是一个键为 QString 类型、值为 QStringList 类型的 QMap。它可以用来存储一组键值对,其中每个键对应一个字符串列表。
例如,我们可以使用 QMap<QString, QStringList> 来存储城市和该城市的所有景点,示例如下:
```
QMap<QString, QStringList> cityAttractions;
cityAttractions["Beijing"] << "Great Wall" << "Forbidden City" << "Summer Palace";
cityAttractions["Shanghai"] << "The Bund" << "Yu Garden" << "Oriental Pearl Tower";
cityAttractions["Guilin"] << "Li River" << "Elephant Trunk Hill" << "Reed Flute Cave";
```
上述代码中,我们首先创建了一个 QMap<QString, QStringList> 对象,然后使用 [] 运算符来访问该对象中的键值对。例如,`cityAttractions["Beijing"]` 返回的是一个 QStringList 对象,我们可以使用 << 运算符向其中添加多个字符串。最终的结果是一个包含三个键值对的 QMap,每个键对应一个字符串列表。
如果需要遍历 QMap<QString, QStringList> 中的所有键值对,可以使用 QMap 的迭代器来实现,示例如下:
```
QMap<QString, QStringList>::const_iterator it;
for (it = cityAttractions.constBegin(); it != cityAttractions.constEnd(); ++it) {
QString city = it.key();
QStringList attractions = it.value();
qDebug() << "City: " << city;
qDebug() << "Attractions: " << attractions;
}
```
上述代码中,我们使用 constBegin() 和 constEnd() 方法获取 QMap 的迭代器,然后使用 key() 和 value() 方法来获取键和值。最终输出每个城市的名称和对应的景点列表。
获取qcombobox的列表为QStringList
你可以使用QComboBox的itemText()函数来获取QComboBox的列表。该函数可以接受一个索引参数,返回该索引对应的条目文本。你可以通过循环遍历所有的索引来获取整个QComboBox的列表,并将每个条目文本添加到一个QStringList中。
以下是一个示例代码:
```cpp
QComboBox comboBox;
// 添加一些条目到comboBox中
QStringList list;
for (int i = 0; i < comboBox.count(); i++) {
QString text = comboBox.itemText(i);
list.append(text);
}
// 现在list中包含了QComboBox的所有条目文本
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)