qvariant

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

此生再无相见时 提交于 2019-12-06 08:22:39
问题 I'm trying to store QList<int> 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 QList<int> : QList<int> list{1,2,3}; QVariant v = QVariant::fromValue<QList<int>>(list); qDebug() << v.type() << "|" << v.typeName() << "|" << v.userType() << "|" << QVariant::typeToName(v.type()); Produces: QVariant::QWidget* | QList<int> | 1030 | QWidget*

Convert QPair to QVariant

空扰寡人 提交于 2019-12-05 03:49:59
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<QString, QVariant> data) . The first value (aka QString ) is used by the receiver as target address, the second contains the data. Now I want to transfer a QPair<int, int> -value, but unfortunately I can not convert a QPair to a QVariant . The optimum would be to be able to transfer a pair of int -values without having to write a new function (or to overload the old one). What is the best alternative for QPair in this case? You have to use the

Casting a list as QVariant or QVariant List

妖精的绣舞 提交于 2019-12-05 00:47:53
My problem is this. I have lists of different numeric types, for example: QList<qreal> 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 simply adding all of the data in mylist to second list QList<QVariant> temp, for example, and passing temp as a paramter. I was wondering if there was any other way to do this. Thank you very much. I've had to do this several times, and to my knowledge the only way is to create a new list. If this is something you have to do frequently with varying

How to sort QList<QVariant> in Qt?

梦想与她 提交于 2019-12-04 23:19:18
I have the following datastructure. QList<QVariant> fieldsList How can I sort this list? This list contains strings. I want to sort the fieldList alphabetically? I would do sorting in the following way: // Compare two variants. bool variantLessThan(const QVariant &v1, const QVariant &v2) { return v1.toString() < v2.toString(); } int doComparison() { [..] QList<QVariant> fieldsList; // Add items to fieldsList. qSort(fieldsList.begin(), fieldsList.end(), variantLessThan); } albertTaberner In Qt5, it seems qSort is deprecated. It's recommended to use: #include <algorithm> QList<QVariant>

QVariant with custom class pointer does not return same address

假如想象 提交于 2019-12-04 09:40:10
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 <QCoreApplication> #include <QDebug> #include <QMetaType> class TestClass { public: TestClass() { qDebug() << "TestClass()::TestClass()"; } }; Q_DECLARE_METATYPE(TestClass*) int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); qDebug() << "metaTypeId =" << qMetaTypeId<TestClass*>(); auto testObject = new TestClass; QVariant variant

How to get the original python data from QVariant

主宰稳场 提交于 2019-12-03 16:32:04
问题 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 the same as data1, 'string'. However in my system data3 is PyQt4.QtCore.QString(u'string') It is not a big deal if the data I want to handle is simple like example, but I want to handle 'dict' type data so I need to fix this problem. I think this is

How to get the original python data from QVariant

三世轮回 提交于 2019-12-03 05:46:19
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 the same as data1, 'string'. However in my system data3 is PyQt4.QtCore.QString(u'string') It is not a big deal if the data I want to handle is simple like example, but I want to handle 'dict' type data so I need to fix this problem. I think this is encoding problem, but can't find how to fix it. *In every document I am declaring that: #-*- coding:

Assigning to nested QVariantMap

强颜欢笑 提交于 2019-11-30 12:53:00
#include <QtCore/QCoreApplication> #include <QVariant> #include <QtDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QVariantMap map; map["foo"] = QVariant(QVariantMap()); map["baz"] = "asdf"; qvariant_cast<QVariantMap>(map["foo"])["bar"] = "a"; qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString(); qDebug() << map["baz"].toString(); return a.exec(); } I am trying to assign to a QVariant within a nested QVariantMap. The first qDebug() outputs nothing, but the second outputs "asdf" as expected. How would I assign the "bar" key in the nested variable map to

Assigning to nested QVariantMap

谁都会走 提交于 2019-11-29 18:07:16
问题 #include <QtCore/QCoreApplication> #include <QVariant> #include <QtDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QVariantMap map; map["foo"] = QVariant(QVariantMap()); map["baz"] = "asdf"; qvariant_cast<QVariantMap>(map["foo"])["bar"] = "a"; qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString(); qDebug() << map["baz"].toString(); return a.exec(); } I am trying to assign to a QVariant within a nested QVariantMap. The first qDebug() outputs nothing,

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

孤人 提交于 2019-11-29 13:36:27
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 when storing them into variant. So far so good. But when i want to test reads and writes of these properties, i need to also know their type. For stuff that are already standard qt types, i can do this via QVariant::type() but as i have alot of usertypes, how would this be accomplished ? From the api of QVariant, i spotted this: bool QVariant: