Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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


  • Lifetime Qt Champion

    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 ?



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


  • Lifetime Qt Champion

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


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


  • Banned

    This post is deleted!

Log in to reply