C ++字符串声明和Qt问题

时间:2016-11-18 10:39:17

标签: c++ qt

cpp文件:

#include "currency.hpp"
currencyNames[4] = {"gbp","usd","eur","aud"};

QComboBox *box1 = new QComboBox();
int i;
for(i=0; i < 4; i++){
    QString *s = QString::string(currencyNames[i]);
    box1->addItem(s);
} 

hpp文件:

#pragma once
#include string

.
.
.

static const int SIZE = 4; 
std::string currencyNames[SIZE];

我不断收到一些错误,我想要一个包含上述信息的数组然后遍历数组,将它添加到QComboBox。没有成功。包括所有相关的Qt标题。

1 个答案:

答案 0 :(得分:1)

除了先前评论中已经陈述的问题之外,QComboBox :: addItem方法接受对QString的引用而不是指针。

由于您决定使用Qt框架,因此您可以使用其集合,从而可以更好地与各种小部件进行互操作。因此,您的示例可以更简单地重写。 例如:

QStringList currencyNames{"gbp","usd","eur","aud"};
QComboBox *box = new QComboBox();
box->addItems(currencyNames);

请记住将框分配给某个父级,以便在适当的时候处理框破坏。

相关问题