Reputation: 1110
I am using ActiveX to kick Excel from Qt and I retrieved the data array using a specific range. The datatype is QVariant but in the debugger it is written "QVariant(QVariantList)". It has around 1000 items, how can I access/get these items ?
Here is the code I produced.
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", filename );
QAxObject* sheets = workbook->querySubObject( "Worksheets" );
QAxObject* sheet = sheets->querySubObject( "Item( int )", 1);
QAxObject* range = sheet->querySubObject("Range(A2:A1002)");
QVariant data = range->dynamicCall("value"); // the data I want to retrieve
Upvotes: 0
Views: 1226
Reputation: 865
QVariant data = range->dynamicCall("value");
QVariantList list = data.toList();
for (/*const*/ auto &v : list) {
/// work with QVariant v
}
From the Qt Docs:
The QVariant class acts like a union for the most common Qt data types.
"most common Qt data types" includes QVariantList. So if you are sure that your QVariant is of type QVariantList, you can use the member function of QVariant to cast it to a QVariantList. If you are not entirely certain that your QVariant is actually a QVariantList, you should use QVariant.canConvert(targetTypeId) to verify before you call QVariant.toList().
Upvotes: 1