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
9
votes
1 answer

How to verify QVariant of type QVariant::UserType is expected type?

I'm writing testing code that will automatically iterate thru all Q_PROPERTY's of widgets and some properties are using types that are registered via qRegisterMetaType. If i want to read/write these into QVariant i need to use QVariant::UserType…
rasjani
  • 7,372
  • 4
  • 22
  • 35
8
votes
3 answers

Convert a QVariant of a custom type to a QString

I have a custom class called Money that I have declared with Q_DECLARE_METATYPE(). class Money { public: Money(double d) { _value = d; } ~Money() {} QString toString() const { return QString(_value); } private: double…
darkadept
  • 647
  • 2
  • 9
  • 23
8
votes
1 answer

From QVariant to Integer and String

The user's entered value can be both: a string or an integer. QAbstractTableModel's setData() method always gets this value as QtCore.QVariant Question: How to implement if/elif/else inside of setData() to distinguish if the received QVariant is a…
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
8
votes
2 answers

Serializing QVariant through QDataStream

I'm possibly writing it wrong but here's the code that i'm trying to execute and it fails to do what's expected: #include #include #include #include int main(){ QByteArray data; QDataStream…
Lex
  • 413
  • 1
  • 7
  • 19
8
votes
1 answer

How to avoid duplicate declarations of Q_DECLARE_METATYPE

My project consists of an app that links to two static libraries. Each of the libraries declares Q_DECLARE_METATYPE< QUuid >, in order to use QUuid with QVariant, which leads to a 'redefinition of struct QMetaTypeId< QUuid >' error. What is the…
szayat
  • 408
  • 3
  • 9
7
votes
4 answers

Is there a reason why QVariant accepts only QList and not QVector nor QLinkedList

QVariant appears to accept QList and not QVector nor QLinkedList. Is it simply because it sees QList, QVector and QLinkedList as fundamentally similar (in an abstract sense) data structures? I'm adding and std::vector…
Alan Turing
  • 12,223
  • 16
  • 74
  • 116
7
votes
2 answers

enum class in QVariant in QSettings

I have a problem with enum classes, QVariants and the QSettings class. There are enum class values that I want to store within a QVariant which goes into a QSettings instance. So, my code actually looks something like this: enum class Foo { …
CppChris
  • 1,226
  • 9
  • 14
6
votes
1 answer

QT SQL datatype (QVariant) mapping for BINARY type of arbitrary length

I have a SQL query( call to a stored procedure to MSSQL) that takes arbitrary length of BINARY type as argument.I am using QT's support for stored procedure. But according to this, there is no corresponding QT type for varbinary for ODBC. QT…
msrepo
  • 63
  • 3
6
votes
2 answers

What is the equivalent of QVariant in C++?

I am trying to port a Qt application to C++ using STL. What is the equivalent of QVariant in C++? QVariant can store any data type - a container that holds heterogenous - different types of objects. However, I have to port this application to C++.…
dexterous
  • 6,422
  • 12
  • 51
  • 99
6
votes
2 answers

How do I get my python object back from a QVariant in PyQt4?

I am creating a subclass of QAbstractItemModel to be displayed in an QTreeView. My index() and parent() function creates the QModelIndex using the QAbstractItemModel inherited function createIndex and providing it the row, column, and data needed.…
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
5
votes
1 answer

Changing and removing values from deeply nested QVariant

I'm using QVariant to manage the project settings of our In-House application. For this I'm using a nested QVariantMap recursively containing QVariantMaps and leaves holding the actual values. Now, I found it quite cumbersome to set and remove nodes…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
5
votes
2 answers

How do I convert QMap > to a QVariant?

QVariant (needed for QSettings class) supports creation from QMap But trying to initialise something like this: QMap)> i; Gives the error: function returning a function. So then I tried…
fenix
  • 215
  • 1
  • 3
  • 8
5
votes
2 answers

Polymorphism with QVariant

I have two classes like this : class Foo { public: Foo(int i) : _i(i) {} int _i; }; Q_DECLARE_METATYPE(Foo*) class Bar : public Foo { public: Bar(int i, int j) : Foo(i), _j(j) {} int _j; }; Q_DECLARE_METATYPE(Bar*) My bench is like…
artoon
  • 729
  • 2
  • 14
  • 41
5
votes
1 answer

QT - How to retrieve QVariant Values from combobox?

I'm using QVariant to store a object inside of a Qcombobox, This appears to work fine. This is the implementing code: Add type to QVariant in header: Q_DECLARE_METATYPE(CDiscRecorder*) pDiscRecorder Casted as CDiscRecorder: CDiscRecorder*…
rreeves
  • 2,408
  • 6
  • 39
  • 53
4
votes
1 answer

QList in QVariant, QVariant::type() returns weird type

I'm trying to store QList in QVariant and then do a typecheck to determine exact type of value stored in QVariant (for serialization). QVariant::type() works fine with scalar types like int or QString, but this is what I got with…
Oleg Antonyan
  • 2,943
  • 3
  • 28
  • 44
1
2
3
12 13