Yaro Kasear
Yaro Kasear

Reputation: 15

QVector not appending!

I am having a problem.

For some reason, despite the fact I have a value in a QString, doing an append() to a QVector isn't working, and I end up still having just an empty vector.

    QList<QVariant> listServers = mapRepos.values ( "servers" ); // Finally, the last data structure, for holding server lists.
    // Populate the repository information with the server lists.

    for ( int i = 0; i < listServers.size(); ++i ) {
      for ( int j = 0; j < listServers.at ( i ).toList().size(); ++j ) {
        std::cout << "On file: " << listServers.at ( i ).toList().at ( j ).toString().toStdString() << std::endl;
        std::cout << "Type #: " << listServers.at ( i ).toList().at ( j ).type() << std::endl;
        buffer3 = listServers.at ( i ).toList().at ( j ).toString();
        std::cout << "In buffer: (" << buffer3.toStdString() << ")" << std::endl;
        arrayRepos.value ( j ).urls.append ( buffer3 );
        std::cout << "In memory: " << arrayRepos.value ( i ).urls.value ( i ).toStdString() << std::endl;
      }
    }
  }

This is the relevant snippet in the source file. The first cout returns a URL, the second URL returns 10 (Which according to the documentations means the QVariant contains a string, which is correct. The third cout outputs the string correctly. Then, the last one outputs no string. Note the presence of the append line there. All my programming common sense tells me that vector should be populated now, but taking a peek at its size shows a size of 0, meaning that QString isn't put in.

The urls QVector is part of a struct:

struct repo {
QString name; // Name of the repository.
QVector<QString> urls; // Address of server or list of mirrors.
};

QString name is part of an earlier bit of code that works okay and is not relevant to this problem.

I can't think of any reason why append() or push_back() wouldn't work.

Upvotes: 1

Views: 1586

Answers (2)

Purnima
Purnima

Reputation: 1022

Can you break up this arrayRepos.value ( j ).urls.append ( buffer3 ); code to individual statements and then try.

Like:

QVector<QString> tmpurls = arrayRepos.value ( j ).urls;
tmpurls.append ( buffer3 );

Upvotes: 0

Begemoth
Begemoth

Reputation: 1389

What is the type of arrayRepos, I guess it is some kind of Qt container (QVector, QList, etc). In this case arrayRepos.value (j) return a copy of a repo structure when you need a reference, thus you need to use either operator[] or at(j) member function.

Upvotes: 2

Related Questions