Easy way to display text alone or with integers in a QTextEdit already existing on the Main ui

  • This is just a clear way to display texts or integers. I found that using the searches i could hardly see a clear way showing how to display text on QTextEdit when button is pressed. The QString::number() allows you to display numbers or integers without errors. The "displayer" found on line 10, is the objectName for QObject ; QTextEdit.
    The click slot is a function. Hope this helps.

    void buttonclick::on_clicked_clicked()

    int x,y,z;
    ui->displayer->setText("The Answer is: " + QString::number(z));


  • Moderators

    If you need to later translate the UI this is the preferred way:
    ui->displayer->setText(tr("The Answer is: %1").arg(QString::number(z)));
    This is because in some languages the number may not come last and translated format string can move the %1 to the right position for the given language.

  • Very true moderator Chris. Just looking at it from a beginners aspect.

  • Would this be faster than using a string stream/QString then setting text or are they doing the same thing ? Also is this the preferred method for setting text dynamically ?

  • A preferred method depends on what you are doing as there are so many approaches to writing a code. I did a method and then the moderator did another. They both fast, it just depends on what you what you displaying. The string stream would be better when you adding a file, so it can't be faster in the case above. The case above features a widget that can display rich text or plain text and it is dynamic. Matter of choice.

  • Well I meant in regards to this context, but I'll take Chris's as the best method as it seems to be the more sensible. Again wanted to know in this context which was faster not to know they are both fast (especially as fast is very subjective).

    So whats wrong with doing this? it does exactly the same as above just with a stringstream so presuming you weren't aware you could do this.
    @std::stringstream test;
    std::stringstream test << Z;


    Which would allow you to display anything dynamically.

    Sadly your response is very bad english so have struggled to understand what you are saying. As I'm currently doing this as we speak, it's important for me to know what is the fastest method of these three.

  • Moderators

    bq. @
    std::stringstream test;
    std::stringstream test << Z;

    This is not a very good way. std::stringstream is not very fast in itself. The str() method creates a temporary std::string object by copying the data of the stringstream. It then takes the data pointer with c_str() and creates a temporary QString object that again copies the data. A lot of unnecessary copies and temporary objects.
    Also note that stringstream is locale aware so it might not be a good fit for some tasks e.g. data serialization.

Log in to reply

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