Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] hello world to textEdit text



  • I am trying to add the variable "world" to the code below but its not working. I would like "world" to be stored in a variable and then add the variable to the textEdit->setText as a new line. for example...

    @QString tt = "<br>world" ;
    tt += ui->textEdit->setText("hello ");@

    I would basically like to add "world" to the textEdit->setText("hello ") so that it would read hello and world on a new line. the above code gives this error: no match for 'operator+=' in 'tt +=...



  • Try this code

    @ui->textEdit->setText(tr("hello %1").arg(tt));@

    That'll show "hello world". Is it what u want?



  • I am looking for something simular to a textEdit->addline feature where every time the textEdit->text is updated, a new line of code is added to it but added to a new line.



  • Do you mean something like "QTextEdit::append":http://doc.qt.nokia.com/latest/qtextedit.html#append ?



  • no. append creates a new line which i want but it appends the first line to the second line, like this...

    hello
    helloworld

    i want just...
    hello
    world

    it would be much easier if i could assign a variable to textEdit like this...
    @tt == ui->textEdit->setHtml("tt");@

    what would be another way for me to code that line above without getting an error?



  • [quote author="kalster" date="1312682966"]no. append creates a new line which i want but it appends the first line to the second line, like this...

    hello
    helloworld[/quote]What code did you use ? Because that works perfectly:
    @ui->textEdit->setText("hello ");
    QString tt = "world";
    ui->textEdit->append(tt);@



  • humm, your right. it does work. I can't remember my code that i used but i must have made a mistake. thank you alexisdm.

    now for my last question. how to get the value of textEdit->setText and store it has a variable. now that i have hello world correctly in the textEdit, i would like to store that result as tt variable. for example...

    @QString tt;
    tt = ui->textEdit->setText();@



  • Have a look at "toHtml()":http://doc.qt.nokia.com/4.7/qtextedit.html#html-prop

    Or you could keep track of what you appended to the QTextEdit yourself in a separate QString.



  • can i have an example on how to use it in a separate Qstring please?


  • Moderators

    Here it is::
    @QString currentText;
    currentText = ui->textEdit->toHtml(); // Or alternatively: currentText = ui->textEdit->toPlainText();
    @

    There also is a method document(), which returns QTextDocument pointer.



  • yes. that is what i was looking for. thank you sierdzio



  • [quote author="kalster" date="1312676984"]
    @QString tt = "<br>world" ;
    tt += ui->textEdit->setText("hello ");@
    [/quote]

    Time to learn some more basic C++ stuff:

    operator += takes two arguments of type QString. One is tt, the other should be your expression. Looking at the API docs of "QTextEdit::setText() ":http://doc.qt.nokia.com/4.7/qtextedit.html#plainText-prop shows you that setText returns void. So there is no second argument to operator += and hence the compiler bails out.

    Methods called setXXX in Qt usually do not return anything. This may be different in other frameworks, but it holds for the vast majority of Qt classes.



  • thank you again Volker for your input. I will read everything at the link you gave me very carefully.


Log in to reply