Qt Error "Index out of Range"

This is an error I get when I try to compile my Qt Project:

ASSERT failure in QList<T>::operator[]: "index out of range", file c:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h, line 477
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.


Apparently the "titles" list is not being populated, thus the "index out of range" error; however, I can't find the reason why "line" is not being appended.
Any Ideas on What Is wrong? I have been trying to figure it out, but I think this is out my league as a beginner. =/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QTime>
#include <QDateTime>
#include <QList>

MovieRec::MovieRec(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MovieRec)
{
    ui->setupUi(this);
    on_searchButton_clicked();
    qsrand(QTime::currentTime().msec()); // Seeds qrand() for more random selection
}

MovieRec::~MovieRec()
{
    delete ui;
}

void MovieRec::getTitle(const QString &file_name, QList<QString> &titles)
{
   QFile myfile(file_name);
   if (myfile.open(QIODevice::ReadOnly))
   {
     QTextStream in(&myfile);
     while (!in.atEnd())
     {
       QString line = in.readLine();
       titles.append(line);
     }
   myfile.close();
   }
}

void MovieRec::on_searchButton_clicked()
{
    int randomValue = qrand() % 100;
    QList<QString> titles;
    if(ui->romanceButton->isChecked())
    {
        getTitle("romance.txt", titles);
    }
    else if(ui->comedyButton->isChecked())
    {
        getTitle("comedy.txt", titles);
    }
    else if(ui->scifiButton->isChecked())
    {
        getTitle("scifi.txt", titles);
    }
    else if(ui->actionButton->isChecked())
    {
        getTitle("action.txt", titles);
    }
    else if(ui->modernButton->isChecked())
    {
        getTitle("classics.txt", titles);
    }
   ui->textBrowser->setPlainText(titles[randomValue]);
}
Last edited on
Topic archived. No new replies allowed.