TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qobjecthelper.cpp
Go to the documentation of this file.
1 /* This file is part of qjson
2  *
3  * Copyright (C) 2009 Till Adam <adam@kde.org>
4  * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
5  * Copyright (C) 2016 Anton Kudryavtsev <a.kudryavtsev@netris.ru>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License version 2.1, as published by the Free Software Foundation.
10  *
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 
24 #include "qobjecthelper.h"
25 
26 #include <QtCore/QMetaObject>
27 #include <QtCore/QMetaProperty>
28 
29 using namespace QJson;
30 
32 {
33 
34 }
35 
36 QVariantMap QObjectHelper::qobject2qvariant(const QObject* object,
37  const QStringList& ignoredProperties)
38 {
39  QVariantMap result;
40  const QMetaObject *metaobject = object->metaObject();
41  int count = metaobject->propertyCount();
42  for (int i=0; i<count; ++i) {
43  QMetaProperty metaproperty = metaobject->property(i);
44  const char *name = metaproperty.name();
45 
46  if (!metaproperty.isReadable() || ignoredProperties.contains(QLatin1String(name)))
47  continue;
48 
49  QVariant value = object->property(name);
50  result[QLatin1String(name)] = value;
51  }
52  return result;
53 }
54 
55 void QObjectHelper::qvariant2qobject(const QVariantMap &variant, QObject* object)
56 {
57  const QMetaObject *metaobject = object->metaObject();
58 
59  for (QVariantMap::const_iterator iter = variant.constBegin(),
60  end = variant.constEnd(); iter != end; ++iter) {
61  int pIdx = metaobject->indexOfProperty( iter.key().toLatin1() );
62 
63  if ( pIdx < 0 ) {
64  continue;
65  }
66 
67  QMetaProperty metaproperty = metaobject->property( pIdx );
68 #if TTK_QT_VERSION_CHECK(6,0,0)
69  const QMetaType type = metaproperty.metaType();
70 #else
71  const QVariant::Type type = metaproperty.type();
72 #endif
73  QVariant v( iter.value() );
74  if ( v.canConvert( type ) ) {
75  v.convert( type );
76  metaproperty.write( object, v );
77  } else if (QLatin1String("QVariant") == QLatin1String(metaproperty.typeName())) {
78  metaproperty.write( object, v );
79  }
80  }
81 }
const char * name
Definition: http_parser.c:458
Namespace used by QJson.
Definition: parser.h:34
static void qvariant2qobject(const QVariantMap &variant, QObject *object)
This method converts a QVariantMap instance into a QObject.
static QVariantMap qobject2qvariant(const QObject *object, const QStringList &ignoredProperties=QStringList(QString(QLatin1String("objectName"))))
This method converts a QObject instance into a QVariantMap.