0
我不明白为什么std::string
在将它传递给构造函数时转换为QString。下面是小例子:在构造函数中将std :: string转换为QString
class StringHandler
{
private:
QString str;
public:
StringHandler(QString s): str(s) {}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string str = "string";
QString qstr(str); // it gives error there are no constructor QString(std::string)
StringHandler handler(QString(str));//it does not give an error. Why?
return a.exec();
}
编辑:
class StringHandler
{
public:
StringHandler(QString s): str(s) {}
QString str;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string str = "string";
StringHandler handler(QString(str));//Here should be error, but there no error. Why?
qDebug()<<handler.str; //Error is here: request for member 'str' in 'handler', which is of non-class type 'StringHandler(QString)'
return a.exec();
}
请添加实际的错误输出。 –
可能重复[如何将字符串转换为QString?](http://stackoverflow.com/questions/1814189/how-to-change-string-into-qstring) – emlai
@zenith问题不是关于错误。我不明白为什么这行:'StringHandler处理程序(QString(str))'工作。 – Leo