[SOLVED] hello world to textEdit text
-
wrote on 7 Aug 2011, 00:29 last edited by
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 +=...
-
wrote on 7 Aug 2011, 00:38 last edited by
Try this code
@ui->textEdit->setText(tr("hello %1").arg(tt));@
That'll show "hello world". Is it what u want?
-
wrote on 7 Aug 2011, 01:13 last edited by
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.
-
wrote on 7 Aug 2011, 01:59 last edited by
Do you mean something like "QTextEdit::append":http://doc.qt.nokia.com/latest/qtextedit.html#append ?
-
wrote on 7 Aug 2011, 02:09 last edited by
no. append creates a new line which i want but it appends the first line to the second line, like this...
hello
helloworldi want just...
hello
worldit 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?
-
wrote on 7 Aug 2011, 02:27 last edited by
[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);@ -
wrote on 7 Aug 2011, 03:03 last edited by
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();@ -
wrote on 7 Aug 2011, 06:10 last edited by
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.
-
wrote on 7 Aug 2011, 06:49 last edited by
can i have an example on how to use it in a separate Qstring please?
-
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.
-
wrote on 7 Aug 2011, 07:22 last edited by
yes. that is what i was looking for. thank you sierdzio
-
wrote on 7 Aug 2011, 11:38 last edited by
[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.
-
wrote on 8 Aug 2011, 02:29 last edited by
thank you again Volker for your input. I will read everything at the link you gave me very carefully.
1/13