How to read a variable in setText()?

Asked

Viewed 112 times

3

I need the text of a lineEdit is equal to a variable string.

I’m using this code:

void MainWindow::on_pushButton_clicked(){

    string texto;
    texto= "mensagem";
    ui->lineEdit->setText(texto);
}

1 answer

3


Do so:

void MainWindow::on_pushButton_clicked(){
    Qstring texto;
    texto = "mensagem";
    ui->lineEdit->setText(texto);
}

Or better yet:

void MainWindow::on_pushButton_clicked(){
    ui->lineEdit->setText(QStringLiteral("mensagem"));
}

I put in the Github for future reference.

The QStringLiteral is a macro present in Qt 5 that creates a literal like QString without having to convert at runtime.

The setText of LineEdit expecting a QString and not a string standard of C++, so it didn’t work.

  • It worked, thank you very much

  • @Samuelbratifavarin if it worked marque the answer as correct, if you do not know how to do this read the tips here on how to use the site: http://meta.pt.stackoverflow.com/q/1078/3635

  • What is the difference between 'Qstring' and Qstringliteral'?

  • @Luke the 1st. is the type and the 2nd. is a macro p/ generate the literal without converting, if you want to know more details, ask a question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.