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
4
votes
2 answers

Is there a way to convert a base type stored in QVariant without specializing the cast?

Let's consider this example: QVariant v1(1); QVariant v2("goofy"); QVariantList list; list << v1 << v2; for (const auto& var : list) { qdebug() << var; // nasty part if (var.type == QVariant::Int) { int value = var.toInt(); …
Moia
  • 2,216
  • 1
  • 12
  • 34
4
votes
2 answers

Qt programming: How to use custom data type in QVariantMap?

I am writing a Qt app that maps a C++ class to Javascript object in QtWebkit. Firstly let me explain what I am trying to do: I have a class inherited from QObject: class myobj : public QObject { Q_OBJECT public: myobj(); …
Mickey Shine
  • 12,187
  • 25
  • 96
  • 148
4
votes
1 answer

QVariantList.append() merges list instead of nesting

When I try to nest a QVariantList inside another QVariantList, the result is the flat merge of the two lists, instead of a sub-list. Demo code: QVariantList container; QVariantList nested() << "bar" << "baz"; container.append("foo"); // or…
Victor Aurélio
  • 2,355
  • 2
  • 24
  • 48
4
votes
1 answer

Why does QVariant::type() return a QVariant::Type when it should be interpreted as a QMetaType::Type?

The docs about QVariant::type() say that: Returns the storage type of the value stored in the variant. Although this function is declared as returning QVariant::Type, the return value should be interpreted as QMetaType::Type. In particular,…
sashoalm
  • 75,001
  • 122
  • 434
  • 781
4
votes
1 answer

QVariant and std::size_t

QVariant does not support std::size_t. What's the proper way to construct a QVariant object using a std::size_t value without losing any platform dependent size restrictions?
Baradé
  • 1,290
  • 1
  • 15
  • 35
4
votes
2 answers

Custom type in QVariant converts to empty string

I'm writing a lexical scanner that generates a stream of tokens from some input. Those tokens have a type and a value. Since I'm using Qt I chose to store the token data as a QVariant. This works pretty well for token data that is of a non-custom…
hochl
  • 12,524
  • 10
  • 53
  • 87
4
votes
4 answers

Converting QList to QVariant

The class contains this: Q_PROPERTY(QList switch1 READ switch1 WRITE setSwitch1 NOTIFY switch1Changed) void setSwitch2(QList arg) { if (m_switch2 != arg) { m_switch2 = arg; emit switch2Changed(arg); …
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
4
votes
1 answer

qvariant as key in qhash

I want to create a data structure with QVariants a keys. It looks like this: QHash, SHAPES::Shape* > _shapes; Unfortunately there is "no matching function for call to ‘qHash(const QVariant&)’". So I defined my own…
Stefan Ramson
  • 531
  • 1
  • 6
  • 18
3
votes
1 answer

How to create a QVariant-based generic model?

Quite often I find myself in need of some custom scheme model, mandating the implementation of more and more models, made even more tedious by the inability of QObject derived classes to be templates. Qt has the QStandardItemModel but that seems a…
dtech
  • 47,916
  • 17
  • 112
  • 190
3
votes
3 answers

generic call of a std::function from QVariantList

I'm looking for a generic way to call a std::function with arguments from a QVariantList. This Version works but has the drawback the template parameters must be specified: template struct VariantFunc { static void…
AndiR
  • 179
  • 10
3
votes
0 answers

Blinking background cell in a model

I am confused about the setData in data methods for my custom role (IsBlinkingRole). I've tried different options but every option failed. bool CustomSqlModel::setData( const QModelIndex& index, const QVariant& value, int role) { if (role ==…
user56383
  • 39
  • 4
3
votes
1 answer

How to convert QList to QVariant?

I can't find a way to convert my QList to a QVariant. There's a constructor QVariant(const QList &val), but no constructor for QList , is it possible to convert directly a QList ?
Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76
3
votes
1 answer

How to hash QVariant?

I need to use QList as a key to std::unordered_map. The purpose of this is to optimize searching over a table of data by making index over the unique key columns. So I made this code. It's not complete, but lists some basic data types that…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
3
votes
1 answer

QVariant's Visitor pattern (without manual type testing and casting)

Does Qt's QVariant class has any existing (and convenient) Visitor pattern implementation? If not, is it possible to achieve something similar to boost::apply_visitor(), i.e. minimize the the duplication in regard to testing the type and casting? I…
sthlm58
  • 1,142
  • 1
  • 9
  • 28
3
votes
2 answers

Create a QVariant from a metatype id

Is there anyway to create a QVariant from a metatype id ? For example : int id = qRegisterMetaType(); QVariant myVariant = QVariant::fromMetaType(id); So myVariant is now a QVariant containing a default-constructed value of "MyStruct". I…
Aurelien
  • 1,032
  • 2
  • 10
  • 24
1 2
3
12 13