qvariant

Qt QDbus Sending Custom Types with QVariant

假如想象 提交于 2019-12-13 04:17:44
问题 I'm trying to send a custom class ( "Span" ) inside a QVariant across the Dbus session bus in Qt between 2 simple applications. Span is a simple class that contains 2 double type properties. I have successfully sent and recovered a QVariant containing just a QString across the dbus interface in the same manner I am trying to do below with a QVariant of a custom class. Span contains the following declaration for the QMETATYPE QVariant registration in the class header file: Q_DECLARE_METATYPE

Can I conveniently converty QVariant back to QList<MyType>?

删除回忆录丶 提交于 2019-12-12 19:16:17
问题 It is possible to convert QList<YourType> to QVariant provided you declare your type as q meta type using this macro: Q_DECLARE_METATYPE(MyType); After that, the conversion is even implicit: QList<MyType> list; QVariant variant = QVariant::fromValue(list); Now my question is how to convert variant back to QList<MyType> . 回答1: QVariant provides method canConvert<T> that you can use to check: if( variant.canConvert<QList<MyType>>() ) { QList<MyType> list = variant.value<QList<MyType>>(); ... }

Best way to access a cpp structure in QML

Deadly 提交于 2019-12-12 08:57:20
问题 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 of what i am trying to do : MyClass.h #include <QObject> #include <QDebug> using namespace std; struct MyStruct { Q_GADGET int m_val; QString m_name1; QString m_name2; QString m_name3; QString m_name4; Q_PROPERTY(int val MEMBER m_val) Q_PROPERTY

qvariant as key in qhash

送分小仙女□ 提交于 2019-12-10 20:24:14
问题 I want to create a data structure with QVariants a keys. It looks like this: QHash<QPair<QVariant, QVariant>, SHAPES::Shape* > _shapes; Unfortunately there is "no matching function for call to ‘qHash(const QVariant&)’". So I defined my own implementation of qHash for QVariants: #pragma once #include <QVariant> #include <QHash> uint qHash( const QVariant & var ) { if ( !var.isValid() || var.isNull() ) //return -1; Q_ASSERT(0); switch ( var.type() ) { case QVariant::Int: return qHash( var.toInt

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

孤人 提交于 2019-12-10 14:10:28
问题 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. Here, for testing purposes, data is a Python string. class TestModel(QAbstractItemModel): def __init__(self): QAbstractItemModel.__init__(self) def index(self, row, column, parent): if parent.isValid(): return self.createIndex(row, column, "bar")

Casting a list as QVariant or QVariant List

不羁的心 提交于 2019-12-10 01:47:34
问题 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. 回答1: I've had to do this several times, and to my

How to sort QList<QVariant> in Qt?

偶尔善良 提交于 2019-12-10 00:57:15
问题 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? 回答1: 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); } 回答2: In Qt5, it

How to use VARIANT* with dynamicCall?

允我心安 提交于 2019-12-08 08:12:19
问题 I'm trying to use a COM object and i'm having problem with the parameter type VARIANT*. I can use the functions of the COM object just fine, except when they have a parameter of this type. The doc generated by generateDocumentation is : QVariantList params = ... object->dynamicCall("GetRanges(int,int,int&, QVariant&)", params); According to the doc provided with the COM object, the parameters should be of type LONG, LONG, LONG* and VARIANT*, and it is precised that the VARIANT* is a pointer

convert function return from ‘std::vector<QString>’ to ‘QVariant’

纵饮孤独 提交于 2019-12-08 07:04:29
问题 I am working on a QT based application.One of of my class is a child class of QAbstractTableModel. The data function has a return type of QVariant(Union).But i want to return a custom type std::vector<QString> Came to know about Q_DECLARE_METATYPE(); It makes new types available to QVariant. -Test Case Code- #include <QApplication> #include <QMetaType> #include <vector> #include<QVariant> Q_DECLARE_METATYPE(std::vector<QString>); QVariant data(int role) { std::vector<QString> test1; test1

Convert QPair to QVariant

你说的曾经没有我的故事 提交于 2019-12-06 22:12:42
问题 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