Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Learning
  3. Qt in Education
  4. QVector <QString> words = lines from the file [index]

QVector <QString> words = lines from the file [index]

Scheduled Pinned Locked Moved Solved Qt in Education
6 Posts 4 Posters 1.5k 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.
  • L Offline
    L Offline
    LordHomie
    wrote on 20 Nov 2019, 22:59 last edited by LordHomie
    #1

    Hi everyone, i just a have a small problem in my Qt project. it is as follows, iam trying to create a vector and assign it to the lines in the text file, so that i can use the button next and previous to go through the lines without getting an error (out of index). but i dont know how to do it actually because iam getting errors everytime i try a new method!

    please, take a look at my code and i will be appreciated.

    My code of .cpp file:

    void thidialog::on_pushButton_2_clicked()
    {
    Quizlet quizlet; //creating an object from the class
    
    ++index; //incrementing here.  I declared ' int index = 0; '   in the header file already
    
    // i declared ' QVector <QString> words;  ' in the header file also
    words = quizlet.ReadAllWords()[index]; //actually here is the problem i guess, iam trying to assign
    //vector words = the function of reading words with index
    
    ui->textEdit->setText( QString::fromUtf8(words.c_str())); //this is for displaying output in the window
    

    Thanks a lot!!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Nov 2019, 23:05 last edited by
      #2

      Hi and welcome to devnet,

      @LordHomie said in QVector <QString> words = lines from the file [index]:

      Quizlet

      What is that class ? What does it do ?

      ui->textEdit->setText( QString::fromUtf8(words.c_str())); //this is for displaying output in the window

      Why are you doing that transformation ?

      As for your code, why aren't you doing bound checks on your index variable ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 20 Nov 2019, 23:27
      2
      • S SGaist
        20 Nov 2019, 23:05

        Hi and welcome to devnet,

        @LordHomie said in QVector <QString> words = lines from the file [index]:

        Quizlet

        What is that class ? What does it do ?

        ui->textEdit->setText( QString::fromUtf8(words.c_str())); //this is for displaying output in the window

        Why are you doing that transformation ?

        As for your code, why aren't you doing bound checks on your index variable ?

        L Offline
        L Offline
        LordHomie
        wrote on 20 Nov 2019, 23:27 last edited by LordHomie
        #3

        @SGaist sorry iam just a beginner, so let me please explain this:

        • Quizlet is a class in my code that has 2 funtions, the first one to add words to the text file.csv and the second one to read these words line by line.

        • about the transformation, it is from a previous code. sorry, i think that i should have not mentioned it but yeah it is not important for now.

        i just want to make a 'next button' to read line by line from the text file. like when i press next, it displays the next line and etc...

        i can do it by incrementing index only but it gives an error because the index is out of range.
        and yeah can you please explain what do you mean by bound checks?

        thank you!

        J 1 Reply Last reply 21 Nov 2019, 05:34
        0
        • L LordHomie
          20 Nov 2019, 23:27

          @SGaist sorry iam just a beginner, so let me please explain this:

          • Quizlet is a class in my code that has 2 funtions, the first one to add words to the text file.csv and the second one to read these words line by line.

          • about the transformation, it is from a previous code. sorry, i think that i should have not mentioned it but yeah it is not important for now.

          i just want to make a 'next button' to read line by line from the text file. like when i press next, it displays the next line and etc...

          i can do it by incrementing index only but it gives an error because the index is out of range.
          and yeah can you please explain what do you mean by bound checks?

          thank you!

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 21 Nov 2019, 05:34 last edited by
          #4

          @LordHomie said in QVector <QString> words = lines from the file [index]:

          but it gives an error because the index is out of range

          Of course if you do not check whether the index is still valid.
          "can you please explain what do you mean by bound checks?":

          ++index;
          if (index < quizlet.ReadAllWords().size())
          {
              words = quizlet.ReadAllWords()[index]; //actually here is the problem i guess, iam trying to assign
              ui->textEdit->setText( QString::fromUtf8(words.c_str()));
          }
          

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

          1 Reply Last reply
          4
          • L Offline
            L Offline
            LordHomie
            wrote on 21 Nov 2019, 09:27 last edited by
            #5

            @SGaist @jsulm
            Thanks a lot people!
            the problem has been solved.
            actually i know how silly the mistake was but well, i have not been practicing c++ for long time but yeah thank you again!!

            1 Reply Last reply
            1
            • M Offline
              M Offline
              marketingwithanoy
              Banned
              wrote on 10 Dec 2019, 07:44 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0

              • Login

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