qt push_back
时间: 2023-12-07 18:05:10 浏览: 28
`push_back`是C++ STL中`vector`容器的成员函数,用于在容器的末尾插入一个元素。在Qt中,`QVector`类也有`push_back`函数,用法与`vector`类似。
例如,如果我们有一个`QVector<int>`类型的容器`vec`,我们可以使用以下代码将一个整数值插入到容器的末尾:
```
vec.push_back(42);
```
这将在`vec`的末尾插入整数值42。注意,`push_back`函数也可以接受其他类型的参数,如对象或字符串。
相关问题
QT push_back
在 Qt 中,`push_back` 不是一个常用的函数,因为它是 C++ STL 中 `vector` 类的函数。在 Qt 中,通常使用 `QList` 或 `QVector` 来替代 `vector`,这两个类都提供了 `append` 函数来添加元素到列表中。
如果你仍然想使用 `push_back` 函数,你可以将 `QList` 或 `QVector` 转换为其底层的 `std::vector` 对象,然后调用 `push_back` 函数。例如:
```cpp
#include <QList>
#include <vector>
int main()
{
QList<int> list = {1, 2, 3};
std::vector<int>& vec = *reinterpret_cast<std::vector<int>*>(&list);
vec.push_back(4);
return 0;
}
```
但是需要注意的是,这种转换并不是官方支持或建议的方法,因此使用时需要谨慎。
C:\Users\22836\Desktop\QT\untitled1\mainwindow.cpp:27: error: No matching member function for call to 'push_back'
这个错误是因为你使用了错误的语法来调用push_back函数。
在C++中,push_back是一个成员函数,需要使用成员访问运算符(`.`)而不是使用函数调用运算符(`()`)来调用它。
修改代码如下:
```cpp
connect(ui->checkBox, &QCheckBox::stateChanged, [=](int state){
qDebug() << state << endl;
if (state == 2) {
strList.push_back(this->ui->checkBox->text());
}
//state 2选中 0未选中
});
```
请确保你在代码中正确使用成员访问运算符(`.`)来调用类的成员函数。这样就能够正确地调用push_back函数将文本添加到strList中了。
相关推荐
![png](https://img-home.csdnimg.cn/images/20210720083516.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)