How to concatenate a variable and two strings



  • Hi,

    I'm trying to concatenate a variable (QString) and two other strings. What I have is a lineEdit and a button, when the button is clicked it should concatenate the content in the lineEdit to the file path and also the .pdf extension and finally open the specified .pdf file.

    This is what I have but it doesn't work.

    @QString fileName;

    fileName = ui->lineEdit->text();

    QDesktopServices::openUrl(QUrl("file:///P:/test/file/" + fileName + ".pdf") );@

    When I qDebug fileName inside the click event I get an empty string "".

    Thanks



  • Try
    @QString("file:///P:/test/file/%1.pdf").arg(fileName)@

    Although I'm not sure what the problem is in your case. Normally concatenating QStrings with the + operator works...

    bq. When I qDebug fileName inside the click event I get an empty string “”.

    Then your lineEdit is empty. (Or did you print the value before assigning it the lineEdit->text()?)



  • bq. Then your lineEdit is empty. (Or did you print the value before assigning it the lineEdit->text()?)

    As always you guys are right, it was my lineEdit.

    You guys are awesome!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.