Invalid signal parameter type: MouseEvent - qt

If I try and use a MouseEvent as an arg in a QML defined signal, I get the following error on load:
Invalid signal parameter type: MouseEvent
There is conflicting information in the Qt docs regarding this, in the QML signal syntax documentation it states that:
The allowed parameter types are the same as those listed under Defining Property Attributes [...] any QML object type can be used as a property type.
Whilst in the QML/C++ interaction documentation it states that:
When a QML object type is used as a signal parameter, the parameter should use var as the type
Setting the argument to use var does work, but this seems unnecessary according to the QML documentation. There was a bug regarding this in the distant past but it was apparently resolved in v5.0.0. So I am doing something wrong, or is this a regression?
Edit
A simple demonstration:
import QtQuick 2.3
Item {
signal sig( MouseEvent mouse )
}

Use QtObject instead
signal sig(QtObject mouse)
Note: This works because QtObject is plain QObject which is base of all Qt objects

Related

Is there a way to detect if a QML property is bound (not set to a static value)?

Is it possible to tell the difference between a property that is set to a static value vs. a property that is bound to something else? I checked documentation, but don't see anything about this.
Rectangle {
id: firstRect
color: "black" // set to static value
}
Rectangle {
color: firstRect.color // bound to external value
}
In this example, can I detect that the firstRect color is a static value, whereas the second rectangle color is bound?
I think what you're after is in a private static method: QQmlPropertyPrivate::binding(). For example here is some QtQuick Designer code using it.
Being technically in the Qt private parts, I'm not sure this helps. Interestingly, the Qt::QML Type has a Qt.binding() function which is used to assign bindings (and apparently calls the above private method behind the scenes). And there is the QML Binding type. But in neither case do I see a way to get an instance of an existing binding (QAbstractBinding) already assigned to a property.
Responding to some of the comments: Through the Qt meta object system you can find what is connected to a property notifier signal. But to do that you'd need to know the QObject and QMetaProperty to which the QML property in question is bound to (or not)... which brings us back to square 1 I believe (finding out what the property is bound to, if anything).

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();

QML QtQuick `control` property of corresponding Style object is sometimes null

I have some QML code using QtQuick.Controls 1.4 from Qt 5.6.1. I have some wrappers around various controls, such as ComboBox; these make use of Style objects. For instance, MyComboBox is a ComboBox with a custom ComboBoxStyle.
Every so often, when certain components are loaded, the control property in the Style object is null:
In MyComboBox.qml:
...
style: ComboBoxStyle {
...
label: Label {
text: control.currentText
...
The resulting error output is:
qrc:/MyComboBox.qml:72: TypeError: Cannot read property 'currentText' of null
Restarting the application typically fixes the error.
I have no idea how this could happen or why the error appears non-deterministic. What can I do to investigate?
This appears to have been fixed in release 5.6.1-1; since upgrading, we have not observed this error. Presumably we were simply observing one of the less-critical symptoms of QTBUG-53761.
Credit to jpnurmi for recognizing the similarity of the symptoms I described to the behavior of the bug, and for notifying me that a release with a fix was available (which, sadly, is not evident on the Qt 5.6 release-series page).

To access QML component in C++ backend

I have a qml file with Rectangle. I would like to trigger the onClicked( ) from C++ back-end.
So, How can I get access of QML component reference in C++/Qt backend?
You should use QObject::findChild() to locate the object, and simply invoke the signal as you would a nominal method.
But there is a catch, as QQuickRectangle itself is a private class, so it is not directly available for use in the C++ API. Also, it doesn't really have a clicked() signal, not unless you implemented one yourself. And if you did, it won't be part of the C++ interface.
Also, there is no onClicked() signal, the signal is clicked() and onClicked: is the handler hook.
However, you can still emit it using the Qt meta system, just use:
QObject * object = engine.rootObjects().at(0)->findChild<QObject *>("yourObjectName");
if (object) QMetaObject::invokeMethod(object, "clicked");
It will work even if the signal is implemented on the QML side, it will work even without casting to the concrete C++ type.
Now, if your object is not directly in the root object tree, you will not be able to find it and will have no choice but to pass a reference to it from the QML side to a C++ slot or invokable function.

Extending Qml in python : named signal parameters

According to Qt's doc :
All Qt signals on a registered class become available as special "signal properties" within QML to which the user can assign a single JavaScript expression. The signal property's name is a transformed version of the Qt signal name: "on" is prepended, and the first letter of the signal name upper cased. For example, the signal used in the example above has the following C++ signature:
signals:
void partyStarted(const QTime &time);
Signal parameters become accessible by name to the assigned script. An unnamed parameter cannot be accessed, so care should be taken to name all the signal parameters in the C++ class declaration. The intrinsic types listed in Adding Types, as well registered object types are permitted as signal parameter types. Using other types is not an error, but the parameter value will not be accessible from script.
"so care should be taken to name all the signal parameters in the C++ class declaration" => How do i do that in python ?
I need this in my attempt to translate this custom C++ extension : http://qt-project.org/doc/note_revisions/44/221/view (WheelArea) into some python/PySide or PyQt code :
So far, i'm almost good, but i can't figure how to translate this part (the wheel event signals) :
signals:
void verticalWheel(int delta);
void horizontalWheel(int delta);
in python (the named signal parameters) to access the delta from qml.

Resources