Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

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

    Qt in Education
    4
    6
    875
    Loading More Posts
    • 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.
    • LordHomie
      LordHomie last edited by LordHomie

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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

        LordHomie 1 Reply Last reply Reply Quote 2
        • LordHomie
          LordHomie @SGaist last edited by LordHomie

          @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!

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @LordHomie last edited by

            @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 Reply Quote 4
            • LordHomie
              LordHomie last edited by

              @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 Reply Quote 1
              • M
                marketingwithanoy Banned last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post