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] return a qstring of a line by lineposition of a plaintextedit
Forum Updated to NodeBB v4.3 + New Features

[solved] return a qstring of a line by lineposition of a plaintextedit

Scheduled Pinned Locked Moved General and Desktop
35 Posts 5 Posters 4.8k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    thistleknot
    wrote on last edited by
    #1

    I'm hoping to load a text file and parse it line by line to read in [token's] kind of xml like.

    I thought the anchorAt returned a QString of a line.

    I thought I could use

    ->blockCount();

    To get the # of lines of the plainTextEdit.

    Apparently that is not the case.

    Is there a way I can read a single line as a QString from a plainTextEdit?

    Can I send it to another container/datatype and process it more easily in another data container?

    Note:
    I have noticed a textCursor function that has the ability to select. Is this what I should be using? I'd hate to do all that selecting myself in front of the user's eyes. I guess I could hide the form?

    Update:
    I think I can do it by sending the data to a qtextstream and following the steps outlined here

    http://stackoverflow.com/questions/15115571/reading-a-txt-file-using-qtextstream-c

    Solved
    QString holderString = ui->plainTextEditLeft->toPlainText();

    QTextStream in(&holderString);
    
    //count lines
    int lineCount = 1;
    *QString line* = in.readLine();
    while (!in.atEnd())
    {
        line = in.readLine();
        lineCount++;
    }
    
    in.resetStatus();
    //resets QTextStream cursor
    in.seek(0);
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      aliks-os
      wrote on last edited by
      #2

      Try put text from QTextEdit to QStringList and than you may take any row which you want. After processing of QStringList, you may put all together and place back to QTextEdit
      @
      QString plainTextEditContents = ui->textEdit->toPlainText();
      QStringList lines = plainTextEditContents.split("\n");
      //some processing
      //...

      //join all in one string
      plainTextEditContents.clear();
      for (int i = 0; i < lines.size(); ++i) {
      plainTextEditContents += lines.at(i);
      if (i!=lines.size()-1)
      plainTextEditContents += "\n";
      }
      ui->textEdit->setText(plainTextEditContents)
      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thistleknot
        wrote on last edited by
        #3

        thanks, I'll backupdate to that for faster access.

        1 Reply Last reply
        0
        • Karoluss96K Offline
          Karoluss96K Offline
          Karoluss96
          wrote on last edited by
          #4

          Hi,
          Thanks @aliks-os, I need the same, but in Python. I don't know how to translate function [my_name].at(i) to Python.

          Could you suggest for different languange?

          JonBJ 1 Reply Last reply
          0
          • Karoluss96K Karoluss96

            Hi,
            Thanks @aliks-os, I need the same, but in Python. I don't know how to translate function [my_name].at(i) to Python.

            Could you suggest for different languange?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Karoluss96 said in [solved] return a qstring of a line by lineposition of a plaintextedit:

            translate function [my_name].at(i) to Python

            Do you mean the plainTextEditContents += lines.at(i); line?

            1 Reply Last reply
            0
            • Karoluss96K Offline
              Karoluss96K Offline
              Karoluss96
              wrote on last edited by Karoluss96
              #6

              Yes, I tried some versions... in friday afternoom :-P

              Now it looks that:

               s=((str(query.record().value("CODE")) + ' - ' + str(query.record().value("STATUS_CONTR"))))
               a=s.split("\n")
               for i in a:
                             print (a) #here I need to put 
              # in the end I suppose:
               self.dlg.plainTextEdit.setPlaceholderText(s)
              #but I'm not sure
              
              JonBJ 1 Reply Last reply
              0
              • Karoluss96K Karoluss96

                Yes, I tried some versions... in friday afternoom :-P

                Now it looks that:

                 s=((str(query.record().value("CODE")) + ' - ' + str(query.record().value("STATUS_CONTR"))))
                 a=s.split("\n")
                 for i in a:
                               print (a) #here I need to put 
                # in the end I suppose:
                 self.dlg.plainTextEdit.setPlaceholderText(s)
                #but I'm not sure
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Karoluss96

                 for i in a:
                               print (a) #here I need to put 
                

                Since i is now a line in a, and since the original code appends each line to plainTextEditContents, did you just try plainTextEditContents += i?

                Actually the effect of the whole C++ loop can be achieved in Python via:

                plainTextEditContents = "\n".join(lines);
                

                (which is just the reverse of the earlier lines = plainTextEditContents.split("\n")).

                Karoluss96K 1 Reply Last reply
                1
                • JonBJ JonB

                  @Karoluss96

                   for i in a:
                                 print (a) #here I need to put 
                  

                  Since i is now a line in a, and since the original code appends each line to plainTextEditContents, did you just try plainTextEditContents += i?

                  Actually the effect of the whole C++ loop can be achieved in Python via:

                  plainTextEditContents = "\n".join(lines);
                  

                  (which is just the reverse of the earlier lines = plainTextEditContents.split("\n")).

                  Karoluss96K Offline
                  Karoluss96K Offline
                  Karoluss96
                  wrote on last edited by
                  #8

                  @JonB I'm not sure that command "+=" is possible in Python... but maybe I've done it wrong

                  JonBJ 1 Reply Last reply
                  0
                  • Karoluss96K Karoluss96

                    @JonB I'm not sure that command "+=" is possible in Python... but maybe I've done it wrong

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Karoluss96

                    I'm not sure that command "+=" is possible in Python... but maybe I've done it wrong

                    It is perfectly legal.

                    But in any case, read the last bit I've added to my response: you don't need to bother with any loop at all in Python.

                    1 Reply Last reply
                    0
                    • Karoluss96K Offline
                      Karoluss96K Offline
                      Karoluss96
                      wrote on last edited by
                      #10

                      OK, now it looks that:

                       s=((str(query.record().value("CODE")) + ' - ' + str(query.record().value("STATUS_CONTR"))))
                       a=s.split("\n")
                       plainText = self.dlg.textEdit.toPlainText()
                       plainText = "\n".join(a)
                       for i in a:
                            plainText += i
                      

                      later I nedd to put all text (not only one record) into textEdit so:

                      self.dlg.plainTextEdit.setPlaceholderText(plainText)
                      

                      shows only the last record, so that loop I think, is compulsory

                      JonBJ 1 Reply Last reply
                      0
                      • Karoluss96K Karoluss96

                        OK, now it looks that:

                         s=((str(query.record().value("CODE")) + ' - ' + str(query.record().value("STATUS_CONTR"))))
                         a=s.split("\n")
                         plainText = self.dlg.textEdit.toPlainText()
                         plainText = "\n".join(a)
                         for i in a:
                              plainText += i
                        

                        later I nedd to put all text (not only one record) into textEdit so:

                        self.dlg.plainTextEdit.setPlaceholderText(plainText)
                        

                        shows only the last record, so that loop I think, is compulsory

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @Karoluss96 said in [solved] return a qstring of a line by lineposition of a plaintextedit:

                        plainText = "\n".join(a)
                        for i in a:
                             plainText += i
                        

                        Please read what I wrote, and take the time to understand what you are doing

                        Actually the effect of the whole C++ loop can be achieved in Python via:

                        you don't need to bother with any loop at all in Python.

                         plainText = self.dlg.textEdit.toPlainText()
                         plainText = "\n".join(a)
                        

                        I don't know what you are intending to do here. The second line completely replaces the value assigned into plainText by the first line.

                        1 Reply Last reply
                        1
                        • Karoluss96K Offline
                          Karoluss96K Offline
                          Karoluss96
                          wrote on last edited by
                          #12

                          The difference of my point is that i don't want to replace 1st value by 2nd but put it down (next) after the first. Finally I need to see:

                          'wait - waiting for data
                          protocol - data in protocol
                          end_of_contr - the end of control '

                          etc.

                          This what you send, works good, but the final show must be as above write

                          JonBJ 1 Reply Last reply
                          0
                          • Karoluss96K Karoluss96

                            The difference of my point is that i don't want to replace 1st value by 2nd but put it down (next) after the first. Finally I need to see:

                            'wait - waiting for data
                            protocol - data in protocol
                            end_of_contr - the end of control '

                            etc.

                            This what you send, works good, but the final show must be as above write

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @Karoluss96
                            I don't understand what you are saying. In any case since you know now that += can be used to append a string to a variable I imagine you can use that in place of = where you want to append.

                            1 Reply Last reply
                            0
                            • Karoluss96K Offline
                              Karoluss96K Offline
                              Karoluss96
                              wrote on last edited by
                              #14

                              @JonB said in [solved] return a qstring of a line by lineposition of a plaintextedit:

                              In any case since you know now that += can be used to append a string to a variable I imagine you can use that in place of = where you want to append.

                              Changing += to = nothing present.
                              Now I have that:

                               for i in a:
                                             plainText = i
                               self.dlg.plainTextEdit.setPlaceholderText(plainText)
                              
                              JonBJ 1 Reply Last reply
                              0
                              • Karoluss96K Karoluss96

                                @JonB said in [solved] return a qstring of a line by lineposition of a plaintextedit:

                                In any case since you know now that += can be used to append a string to a variable I imagine you can use that in place of = where you want to append.

                                Changing += to = nothing present.
                                Now I have that:

                                 for i in a:
                                               plainText = i
                                 self.dlg.plainTextEdit.setPlaceholderText(plainText)
                                
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @Karoluss96 said in [solved] return a qstring of a line by lineposition of a plaintextedit:

                                Changing += to = nothing present.

                                What does this mean?

                                 for i in a:
                                               plainText = i
                                

                                What do you think this achieves? Please review assigning to versus appending to a variable, this is basic Python stuff. You have all the information you need.

                                1 Reply Last reply
                                0
                                • Karoluss96K Offline
                                  Karoluss96K Offline
                                  Karoluss96
                                  wrote on last edited by
                                  #16

                                  I return to "+=".
                                  Now I try to put from begin in this forum code:
                                  if (i!=lines.size()-1)
                                  plainTextEditContents += "\n";

                                  but I don't know which python function is equivalent for C++'s .size()

                                  JonBJ jsulmJ 2 Replies Last reply
                                  0
                                  • Karoluss96K Karoluss96

                                    I return to "+=".
                                    Now I try to put from begin in this forum code:
                                    if (i!=lines.size()-1)
                                    plainTextEditContents += "\n";

                                    but I don't know which python function is equivalent for C++'s .size()

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #17

                                    @Karoluss96

                                    • First, I already showed you that "\n".join(lines) does the whole of what the for loop does, without any need to build it line by line or look at how may elements are in lines. Now you return to not using it.

                                    • If you insist on doing it your slow way, look up what C++ QList::size() returns and then use your knowledge of Python to call its method for returning the number of items in a Python list.

                                    Sorry, but you are making a mountain out of mole hill for this and not acting properly on suggestions. I leave you to it....

                                    1 Reply Last reply
                                    1
                                    • Karoluss96K Karoluss96

                                      I return to "+=".
                                      Now I try to put from begin in this forum code:
                                      if (i!=lines.size()-1)
                                      plainTextEditContents += "\n";

                                      but I don't know which python function is equivalent for C++'s .size()

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Karoluss96 Please take a look at the code you posted and think about what you are doing:

                                      plainText = "\n".join(a)
                                      for i in a:
                                           plainText += i
                                      

                                      Don't you think that you're doing same thing two times?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • Karoluss96K Offline
                                        Karoluss96K Offline
                                        Karoluss96
                                        wrote on last edited by
                                        #19

                                        Yes It true :-D , but I still I don't know how to put every line from sql question into text Edit, like I show in one of posts above

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • Karoluss96K Karoluss96

                                          Yes It true :-D , but I still I don't know how to put every line from sql question into text Edit, like I show in one of posts above

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @Karoluss96 Do you mean this:

                                          self.dlg.plainTextEdit.setPlaceholderText(plainText)
                                          

                                          ? If so it should work already. Or do you want to set normal text (not placeholder)?

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          1

                                          • Login

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