QMap datatype in QML - qt

I am using Qt 5.9.4 opensource. Is there any QML type similar to QMap?
Currently I am using a C++, which is holding a QMap. Using Q_INVOKABLE methods, retrieving information from QMap in the QML code.
Question:
Will this create any performance issue, as i m retrieving data from C++ in QML? Is there any way to have map datatype in QML itself?

You can simply use JavaScript objects - they are close to maps/dictionaries, for example:
var myMap = {x: "Test", y: "Test 2"};
myMap["z"] = "Test 3"
console.log(JSON.stringify(myMap))
outputs {"x":"Test","y":"Test 2","z":"Test 3"}.
Note: JSON.stringify is used just to make output more readable, it is not a part of my solution.

If you make your QMap a QProperty you will be able to access it directly from the qml. As far as any performance concerns this documentation covers that nicely in regards to QProperties. https://doc.qt.io/qt-5/qtquick-performance.html

Related

How to get the typename of a QObject instance parsing QML?

I have got a QQuickView which has loaded a qml file like the following.
Rectangle { width: 100; height: 100 }
Then I am retrieving the root object via QObject *root = view->rootObject().
Now I want to get the class name from this object.
The following code results into "QQuickRectangle"
root->metaObject()->className()
But what I want is "Rectangle" just like the typename in the qml file.
Any idea?
Edit: I want to build a treeview with the object hirarchie of a qml file like QtCreator.
There is a pattern there, for qml types implemented in C++ the name would be QQuickSomething, for qml types implemented in qml the name would be Something_QMLTYPE_X_MAYBEMORESTUFF(objAddress).
So you can do some basic string editing depending on the result you get to isolate the actual type name:
QString name = QString(root->metaObject()->className());
if (name.contains("QQuick")) name.remove("QQuick");
else if (name.contains("QMLTYPE")) name.remove(QRegExp("_QMLTYPE_[0-9]*.*"));
// else it might be just a QObject or your on custom type you should handle
Edit: I want to build a treeview with the object hirarchie of a qml
file like QtCreator.
Unless you are willing to dig into and use private APIs, it would likely be easier and also more useful to have your own custom model to drive both a view and the actual object tree. Also, QML is quite easy to parse, I'd personally buckle down and write a parses faster than it would take me to get into the existing one, especially if all that is needed is an object tree outline, but YMMV.
There is "better" information kept on this (QQmlType & QQmlMetaType), but it is not accessible through any public API that I can think of.
Can you explain what you would like to do with it? Maybe there's an alternative.
QtQuick doesn't provide some special metadata for QML items. It looks that QtQuick uses item types internally only while parsing the source.
The known workaround is objectName:
Rectangle {
objectName: "Rectangle"
}
and so:
QString className = item->objectName();

QMap with multifields

I need to store some data of table type like a QTableWidget but without a GUI. Something along the line of the following code:
QMap<QString, QString, int, QString, int>
Is there a way of achieve this in Qt? My Qt version is 5.3.
You seem to be unclear on a few concepts.
A map (also known in some languages as a dictionary) is an associative array. It associates a key to a value, that's about it, there are no "fields" involved whatsoever, just a key and a value.
There is no data type in Qt to model a database table. For such tasks you usually directly use SQL, Qt supports SQL with various different database drivers.
If you don't want to use a database but instead want to have "native" C++ types, you can simply create an object with all the desired fields:
struct Entry {
QString s1, s2, s3;
int i1, i2;
};
And then put that into whatever container you want.
QList<Entry> entryList;
QVector<Entry> entryVec;
QSet<Entry> entrySet;
You can wrap the container in a QAbstractListModel, implement the key functions and roles and have that model be used for a table widget or a QML view.

QML map binding v2

I'm complete noob in Qt, so my question may sound too stupid, but I really need help. I know C++ a little and that's it.
So, my task is to write a C++ program which reads INI-alike (format is not very important) file
height=20
width=15
To make it clear, I have no idea what properties will be defined in this file, names or types are unknown to me at compile time.
After that program loads QML file (I can do this) and injects loaded file data (have no idea how to do this) as JavaScript object, for instance named "Settings", so that QML property bindings will use it like
Rectangle {
width: Settings.width
height: Settings.height
}
So the question is: How can I inject read data as JavaScript object into QML so that QML property binding will use it?
One way to do it would be to write a QObject wrapper around QSettings and expose an instance of it to QML, another would be to use the Settings QML Type. I am sure there are others.

QML map binding

I'm complete noob in Qt, so my question may sound too stupid, but I really need help. I know C++ a little and that's it.
So, my task is to write a C++ program which reads INI-alike file (I can do this, but not sure of most correct/Qt way)
height=20
width=15
or
height=int:20
width=int:15
if properties should be strongly typed. File format is not very important. To make it clear, I have no idea what properties will be defined in this file, names or types are unknown to me at compile time.
after that program loads QML file (I can do this) and injects loaded file data (have no idea how to do this) as JavaScript object, for instance named "Settings", so that QML property bindings will use it like
Rectangle {
width: Settings.width
}
So the questions are:
What is most correct/qt-style way to read INI-alike file?
How can I inject read data as JavaScript object into QML so that QML property binding will use it?
First: The most Qt-style way is using QSettings class:
QSettings *settings = new QSettings("G:/options1.ini",QSettings::IniFormat);
qDebug()<< "height" <<settings->value("height").toInt();
qDebug()<< "width" <<settings->value("width").toInt();
My file:
height=20
width=15
Output:
height 20
width 15
See description of this class. It is really helpful thing.
http://qt-project.org/doc/qt-4.8/qsettings.html

how to pass qmap from qt to javascript?

I could not find the sample code about passing qmap to javascript in qt.
what I did ?
I have connected a QObject to the javascript, and could able to emit the signal from qt app, which
is captured by javascript.
emit mydata(mapVariable);
In JavaScript:
in the alert I am trying to print like:
function myslot(mapVar) {
alert (" map variable " + mapVar);
}
Problem:
It is not printing anything. That shows fundamentally I am making a big blunder here. Kindly suggest me how to resolve this issue?..
Well,
I found the solution, The QVariantMap is the data structure which can be passed to the java script, and can be treated as a JSON in the java script side.

Resources