How to read a variable in setText ()?

3

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

I'm using this code:

void MainWindow::on_pushButton_clicked(){

    string texto;
    texto= "mensagem";
    ui->lineEdit->setText(texto);
}
    
asked by anonymous 28.11.2015 / 23:46

1 answer

3

Do this:

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

Or even better:

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

The QStringLiteral is a macro present in Qt 5 that creates a literal of type QString without having to do conversion at runtime.

The setText of LineEdit expects a QString and not a string default of C ++, so it did not work.

    
29.11.2015 / 00:00