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.

166 questions
22
votes
3 answers

How to support comparisons for QVariant objects containing a custom type?

According to the Qt documentation, QVariant::operator== does not work as one might expect if the variant contains a custom type: bool QVariant::operator== ( const QVariant & v ) const Compares this QVariant with v and returns true if they are…
Tyler McHenry
  • 68,965
  • 15
  • 114
  • 158
21
votes
3 answers

How to sort QList in Qt?

I have the following datastructure. QList fieldsList How can I sort this list? This list contains strings. I want to sort the fieldList alphabetically?
dexterous
  • 5,684
  • 9
  • 42
  • 84
16
votes
2 answers

What is a QVariant and when should it be used?

What is a QVariant and when should it be used?
amiref
  • 2,565
  • 5
  • 27
  • 43
16
votes
2 answers

How can I cast a QVariant to custom class?

I'm developing a BlackBerry 10 mobile application using the Momentics IDE (native SDK). I have a listview which I want to handle its items click with C++ (I need to use C++ not QML). I can get the index path using the "connect" instruction, but I…
J.M.J
  • 1,081
  • 3
  • 18
  • 35
16
votes
1 answer

QVariant to QIcon/QPixmap/QImage

I want to extract a QIcon I've stored in one of a QTreeWidget's columns, as Qt::DecorationRole. QTreeWidgetItem *item = ui->treeWidget->topLevelItem(index); const QIcon &icon = item->data(0, Qt::DecorationRole)._howToConvert_(); However, I can only…
sashoalm
  • 63,456
  • 96
  • 348
  • 677
15
votes
3 answers

How to get the original python data from QVariant

I am just learning python and Qt these days. So please consider that this will be a newbie question, but I am stuck here. import sys from PyQt4.QtCore import * data1 = 'string' data2 = QVariant(data1) data3 = data2.toPyObject() I expected data3 is…
Jonas Hong
  • 275
  • 1
  • 2
  • 7
14
votes
1 answer

QVariant comparison with own types working?

Update I have created an qt bugticket hoping the documentation will be extended. Original Question Believing an Question from 2010 and the Qt Documentation, the operator==() doesn't work with custom types. Quote: bool QVariant::operator==(const…
Random Citizen
  • 1,086
  • 3
  • 13
  • 28
12
votes
1 answer

Casting a list as QVariant or QVariant List

My problem is this. I have lists of different numeric types, for example: QList mylist; Now, in my code I have a function that that expects a QVariant argument which is mylist. The only way I've found to do this is by using a for cyle and…
aarelovich
  • 4,116
  • 6
  • 41
  • 79
11
votes
1 answer

Converting QVariant to a QStringList

I have stored QStringList in a QVariant variable while calling setData function. Now I'd like to restore this data from QVariant variable. How to do it? Edit: I tried convert but I don't seem to understand how it works.
smallB
  • 14,808
  • 29
  • 97
  • 144
11
votes
1 answer

How QVariant Works Internally?

I want to know, How QVariant can internally stores, int, QMap, QList,... I mean what is the internal data-structure/Implementation? What is the overhead of storing and retrieving types (int, float) in QVariant?
SunnyShah
  • 24,498
  • 28
  • 81
  • 130
11
votes
5 answers

Assigning to nested QVariantMap

#include #include #include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QVariantMap map; map["foo"] = QVariant(QVariantMap()); map["baz"] = "asdf"; …
Jeff
  • 148
  • 1
  • 7
10
votes
2 answers

Convert QPair to QVariant

I have the following problem: I want to transmitt data via TCP, and wrote a function for that. For maximum reusability the function template is f(QPair data). The first value (aka QString) is used by the receiver as target…
arc_lupus
  • 3,317
  • 4
  • 36
  • 66
10
votes
3 answers

How to convert an unsigned long int to QVariant

I have realized that QVariant does not offer functionality for long and unsigned long. It offers conversions to int, unsigned int, long long and unsigned long long. We can find in current Desktop architectures that long and int are equivalent, but…
Antonio
  • 783
  • 1
  • 7
  • 17
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
  • 606
  • 2
  • 7
  • 23
8
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
  • 6,075
  • 3
  • 18
  • 32
1
2 3
11 12