Problem with appending to QVector, when QVector is a private field.
-
wrote on 18 Aug 2011, 13:50 last edited by
Hello,
I wrote a simple class to retrieve a file and prepare data using regexs etc. When I retrieve a file, every line should be append to QVector <QString>
@
#include "dispatcher.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>Dispatcher::Dispatcher()
{
this->file_name = "sample.cpp";
this->data = QVector<QString>();
this->prepared = "";
}void Dispatcher::retrieveFile(QString &path)
{
QFile file(path);
QTextStream in(&file);
QString line;if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "An error has occured by opening file: " << path << endl; } while (!in.atEnd()) { line = in.readLine(); qDebug() << line; this->data.append(line); }
}
int Dispatcher::getDataSize()
{
return this->data.size();
}QString Dispatcher::getDataAt(int i)
{
if(this->data.size() <= i){
return this->data.at(i);
} else return QString("");
}@
And this is part of main.cpp:
@
Dispatcher* d = new Dispatcher();
d->retrieveFile(pattern_path);for(int i = 0; i < d->getDataSize(); i++){ qDebug() << d->getDataAt(i); }
@
And the output is like: "" "" "" .... Only empty QStrings. I even checkd with qDebug() if retrieveing lines aren't empty and they aren't.
I'll be very glad for all hints how to resolve this problem.
greentings,
piotrekd -
wrote on 18 Aug 2011, 14:33 last edited by
You should change line 39 above to
@
if(i < this->data.size() ) {
@ -
wrote on 18 Aug 2011, 14:36 last edited by
Thanks, that was it;)
-
wrote on 18 Aug 2011, 14:37 last edited by
No problem. Those are often the most tricky bugs to spot when you're reading your own code. :-)
-
wrote on 18 Aug 2011, 21:22 last edited by
Just another little hint: line 10 is not necessary. The default constructor is called by default from the autogenerated Dispatcher constructor, so there is no need to construct another object and throw away the first.
If you're not bound to use QVector, you might want to migrate to [[Doc:QStringList]], just in case you didn't know of its existence yet :-)
-
wrote on 18 Aug 2011, 21:31 last edited by
Thanks for advice:) I heard about QStringList but I had never an opportunity to use it. However, I'll check QStringList out and mayby I'll make a switch in my project :)
1/6