[英]Hello World Qt creator
I'm trying to make a simple dialog box showing my name. 我正在尝试制作一个显示我的名字的简单对话框。 Look at the code.
看代码。
Pessoa *p = new Pessoa("Ronald Araújo", "ronald.araujo@live.com", 23);
QMessageBox msg; msg.setText(QString::fromUtf8(p->getNome()));
msg.exec();
But the code breaks in the line of setText () with the following error: 但是代码在setText()行中中断,并出现以下错误:
error: no matching function for call to 'QString::fromUtf8(std::string)'
msg.setText(QString::fromUtf8(p->getNome));
Remembering that when I put for example msg.setText(QString::fromUtf8("Hi World"))
the code runs normally. 请记住,当我输入例如
msg.setText(QString::fromUtf8("Hi World"))
,代码将正常运行。
Implementation to return the name: 执行返回名称:
string Pessoa::getNome(){ return this->nome; }
QString
s can't be constructed from std::string
directly. QString
不能直接从std::string
构造。 You have two options that I can think of immediately: 您可以立即想到两个选择:
Either change 要么改变
string Pessoa::getNome(){ return this->nome; }
to 至
QString Pessoa::getNome(){ return this->nome; }
or change 或改变
QMessageBox msg;
msg.setText(QString::fromUtf8(p->getNome()));
msg.exec();
to 至
QMessageBox msg;
msg.setText(QString::fromUtf8(QString::fromStdString(p->getNome())));
msg.exec();
Have a look at the documentation of QString::fromUtf8()
: 看看
QString::fromUtf8()
的文档:
QString QString::fromUtf8( const char * str, int size = -1 )
It does not take a std::string
as its first argument. 它不将
std::string
作为其第一个参数。 You can either use QString::fromStdString()
您可以使用
QString::fromStdString()
QString QString::fromStdString( const std::string & str )
which does take an std::string
, or change your code to provide the char array
of the string: 它确实需要一个
std::string
,或更改您的代码以提供字符串的char array
:
msg.setText( QString::fromUtf8( p->getNome().c_str() ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.