Home > OS >  c qt can i fill a QVariant with multiple struct objects
c qt can i fill a QVariant with multiple struct objects

Time:02-16

I have the following struct:

struct FileInfo {
QString fileName;
QString fileSize;
QString md5Sum;

};

is it possible to put approximately 30 such objects into a QVariant and then be able to iterate through the QVariant to retrieve one of the structs based on index and then cast the object back into a struct and query the struct for say fileSize of a specific file? Is there a better approach to my issue?

CodePudding user response:

You can consider QVariantList to store object as QVariant. Of course, you can convert back into your custom struct.

Example.

struct FileInfo {
    QString fileName;
    QString fileSize;
    QString md5sum;
};

int main()
{

    QVariantList variantList;
    for(int i = 0; i < 30; i  ) {
        FileInfo info{QString("File %1").arg(i), QString("size %1").arg(i), QString("md5sum %1").arg(i)};
        QVariant var;
        var.setValue(info);
        variantList.append(var);
    }

    for (auto v: variantList) {
        FileInfo f = v.value<FileInfo>();
        qDebug() << "File name: " << f.fileName << "\tfile size: " << f.fileSize;
    }

    return 0;
}
  • Related