Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with appending to QVector, when QVector is a private field.
Forum Updated to NodeBB v4.3 + New Features

Problem with appending to QVector, when QVector is a private field.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.2k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    piotrekd
    wrote on last edited by
    #1

    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&#41;)
    {
        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(&#41;; i++&#41;{
        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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      You should change line 39 above to
      @
      if(i < this->data.size() ) {
      @

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        piotrekd
        wrote on last edited by
        #3

        Thanks, that was it;)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          No problem. Those are often the most tricky bugs to spot when you're reading your own code. :-)

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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 :-)

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • P Offline
              P Offline
              piotrekd
              wrote on last edited by
              #6

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