Questions tagged [qvariant]

QVariant is a data type in Qt library, that acts as a "container" for most of the common Qt data types.

QVariant can contain most of the basic Qt data types, and perform easy conversions between them. QVariant can also contain tree-like structures, which is shown in this small example:

//Creating a list of QVariants.
QVariantList myList;
//Creating a single QVariant variable
QVariant var;
//Populating the list with different types
myList << "Hello, world!";
myList << 15;
myList << 0x0A;
//After the following assignment, the variable will hold the list.
var = myList;

The official Qt documentation can be found here for Qt 4.8 and here for Qt 5.

181 questions
3
votes
1 answer

Qt : from unsigned long long to QJsonObject

Is it possible to use long long as a value at QJsonObject ? I was forced to change my API from JSON to XML because 1 field I got had BigInt values and aparently I can't extract big numbers from QJsonValue. Here's my peace of code that may show what…
Rafael Fontes
  • 1,195
  • 11
  • 19
3
votes
2 answers

QVariant vs boost::any vs boost::variant

I need efficient way to store values of different types (int, float, QString or std::string, bool) in one of "generic" containers like QVariant. I want to archive less memory usage. I prefer a container that doesn't store type of the internal value…
tmporaries
  • 1,523
  • 8
  • 25
  • 39
3
votes
3 answers

Saving 64-bit integer with QSettings

Is there any neat way, short of converting number to QByteArray, to save quint64 with QSettings? The problem is QVariant doesn't accept qint64 nor quint64.
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
3
votes
1 answer

qVariantValue is "QT_DEPRECATED" - what is the replacement?

i need to convert Qt legacy code from 4 to 5.1 now i have compilation error in visual studio 2010 : SingleItem* item = qVariantValue(index.data()); gives me : .cpp(63): error C2065: 'qVariantValue' : undeclared identifier when i…
user63898
  • 29,839
  • 85
  • 272
  • 514
2
votes
0 answers

How to compare QVariant values in Qt 5.15?

Qt 5.15 has QVariant compare operators deprecated: #if QT_DEPRECATED_SINCE(5, 15) QT_DEPRECATED inline bool operator<(const QVariant &v) const { return compare(v) < 0; } QT_DEPRECATED inline bool operator<=(const QVariant &v) const {…
hyde
  • 60,639
  • 21
  • 115
  • 176
2
votes
1 answer

Qt enums comparison and output to QDebug

i am registering enums to the Qt meta-object system. i have double checked that all of those have a meta-type ID, and everything looks perfect. But i have some kind of unresolved issue with the comparison of those. Let's consider this code for…
Doodloo
  • 869
  • 5
  • 18
2
votes
0 answers

How can I reference the value of a QVariant without making a copy?

As part of parsing a JSON response, we are traversing a tree of QVariantMaps. To my understanding, we are creating a copy at each level by calling QVariant::toMap() or qvariant_cast(). I would like to optimize this process however I…
Joel Barr
  • 21
  • 2
2
votes
0 answers

How to get QVariant value in QML

I run script file with QQmlEngine. I passing QVector3D value to qml (QQmlEngine): QVector3D dir(1,2,3); QQmlEngine lifeEngine; lifeEngine.globalObject().setProperty("direction", lifeEngine.toScriptValue(dir)); This QML code outputs…
morpheus
  • 31
  • 3
2
votes
1 answer

How to save/serialize QVariant that is QVector

I'm lost as to how to fix this : qRegisterMetaType>("QVector"); QMap wah; wah[0] = QVariant::fromValue(QVector{12, 231, 45, 125, 123, 12, 312, 4, 12}); qDebug() << wah; QByteArray ar; …
Dariusz
  • 960
  • 13
  • 36
2
votes
1 answer

Add private sub class to Q_DECLARE_METATYPE

How to add MySubClass to Q_DECLARE_METATYPE. This is sample code and please ignoring business of code. #include #include #include namespace MyNS { class MyClass : public QObject { public: MyClass() :…
2
votes
1 answer

Why is QVariant treating char type as an integer type?

I am trying to write a code snippet which would take a QList of QVariants and would populate the list using ListView. Use cases with types QString, int, bool, double etc. are successful. However, when I am trying to pass char data type as list…
VivekVar
  • 69
  • 5
2
votes
1 answer

receiving unknown templated object from QVariant

I have a variadic template class for keeping answers from own net class enum class TaskType {readReg, writeReg, readRnd, writeRnd, readBlock, writeBlock, pause}; template class Reply { public: Reply(TaskType t, Args...params):…
Greadimar
  • 81
  • 8
2
votes
1 answer

QTreemodel multiple QVariant role

I am using this Example http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html and need to pass a Color as Forgoundroll to the data, but cant figure it out. In the treemodel.cpp i have altered the data as following.. QVariant…
BobR
  • 85
  • 6
2
votes
1 answer

How to return QVariant type array

I am recently creating a model for qml with c++, but I face a problem when returning a QVariant type empty array. How should I define my return statement? switch (role) { case NameRole: return QVariant(QStringLiteral("AAAAA")); case LevelRole: …
Changlin
  • 39
  • 5
2
votes
1 answer

Change background color for a cell in a QTableModel

I want to color a cell in a QTableView. So I'm trying to change the itemData of the corresponding item in the associated QTableModel. To do so, I use the setItemData method of the QAbstractTableModel class. In the documentation…
V Damoy
  • 190
  • 10