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
26
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
  • 6,422
  • 12
  • 51
  • 99
23
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…
Mohamed Jihed Jaouadi
  • 1,427
  • 4
  • 25
  • 44
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
  • 74,820
  • 18
  • 121
  • 166
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
  • 3,181
  • 7
  • 38
  • 62
16
votes
2 answers

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
  • 75,001
  • 122
  • 434
  • 781
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,272
  • 3
  • 14
  • 28
13
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
  • 16,662
  • 33
  • 107
  • 151
13
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,942
  • 5
  • 45
  • 81
13
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
  • 851
  • 2
  • 8
  • 17
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
  • 5,140
  • 11
  • 55
  • 106
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
  • 28,934
  • 30
  • 90
  • 137
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
  • 1
  • 7
10
votes
2 answers

Best way to access a cpp structure in QML

I need to pass structures between cpp and QML. If i use property i should create an individual set and get functions, My structure contains minimum 5 members so i felt it's not good to use set and get for all those members. Following is an example…
pra7
  • 834
  • 2
  • 21
  • 50
9
votes
2 answers

QVariant with custom class pointer does not return same address

I need to assign a pointer to a custom class in qml using QQmlContext::setContextProperty(). Another qml object has Q_PROPERTY of the same type to retrieve it again. A simple test showed me that the conversion does not work like i thought. #include…
feedc0de
  • 3,646
  • 8
  • 30
  • 55
1
2 3
12 13