tr0yspradling
tr0yspradling

Reputation: 604

Qt loop through QHash<QString,QString> to return it's key-value pairs

I'm trying to loop through a QHash using foreach and get each pair in the QHash and then get the keys and values of those so I can append them to a string.

Here's what I have

QString Packet::Serialize() {
    QString sBuilder = Command.toUpper() + " ";

    foreach(QMap<QString,QString> pair, Attributes) {
        sBuilder.append(pair); // i know this isn't right because I couldn't
                               // finish the statement 
    }
}

The variable Attributes is the QHash.

Also, I realize the code is probably 100% wrong because I'm converting it from C#.

Upvotes: 1

Views: 7808

Answers (1)

Chris
Chris

Reputation: 17535

Looks like you're trying to append each key/value pair to a string? Something like this would work:

QStringList data;
foreach(const QString &key, Attributes.keys())
    data << key << Attributes.value(key);
sBuilder += data.join(" ");

Upvotes: 2

Related Questions