简体   繁体   中英

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:

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.

Implementation to return the name:

string Pessoa::getNome(){ return this->nome; }

QString s can't be constructed from std::string directly. 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 QString::fromUtf8( const char * str, int size = -1 )

It does not take a std::string as its first argument. You can either use 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:

msg.setText( QString::fromUtf8( p->getNome().c_str() ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM