Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] hello world to textEdit text
QtWS25 Last Chance

[SOLVED] hello world to textEdit text

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 19.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kalster
    wrote on 7 Aug 2011, 00:29 last edited by
    #1

    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 +=...

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zither
      wrote on 7 Aug 2011, 00:38 last edited by
      #2

      Try this code

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

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on 7 Aug 2011, 01:13 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alexisdm
          wrote on 7 Aug 2011, 01:59 last edited by
          #4

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on 7 Aug 2011, 02:09 last edited by
            #5

            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?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alexisdm
              wrote on 7 Aug 2011, 02:27 last edited by
              #6

              [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);@

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalster
                wrote on 7 Aug 2011, 03:03 last edited by
                #7

                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();@

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  Eddy
                  wrote on 7 Aug 2011, 06:10 last edited by
                  #8

                  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.

                  Qt Certified Specialist
                  www.edalsolutions.be

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on 7 Aug 2011, 06:49 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sierdzio
                      Moderators
                      wrote on 7 Aug 2011, 06:59 last edited by
                      #10

                      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.

                      (Z(:^

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kalster
                        wrote on 7 Aug 2011, 07:22 last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on 7 Aug 2011, 11:38 last edited by
                          #12

                          [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.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kalster
                            wrote on 8 Aug 2011, 02:29 last edited by
                            #13

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

                            1 Reply Last reply
                            0

                            1/13

                            7 Aug 2011, 00:29

                            • Login

                            • Login or register to search.
                            1 out of 13
                            • First post
                              1/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved