QtQuick and C++ or QML + Javascript ?
-
Thanks a lot for the great explanation @jeremy_k !
You cast some light on several points, really usefulNow what i understand is when you do something like
Q_PROPERTY(int pixelWidth READ pixelWidth WRITE setPixelWidth NOTIFY pixelWidthChanged)
then using it in QML
text: Worker.pixelWidth // this is calling the getter function onClicked: Worker.pixelWidth = 1920 // this is calling the setter function
Which is not obvious when you start using C++ bindings on QML
I then use Q_INVOKABLE to trig some functions to do some process or calculations.
One thing i still don't get : i can't use any C++ signal in QML
I thought when using NOTIFY keyword in Q_PROPERTY i could then use it in my QML likeonPixelWidthChanged: // make something
which is not the case
You can find the updated code here : https://gist.github.com/martialgallorini/66a486aea1f4ce884dc4
thanks a lot
It looks like the Worker code is missing an explicit emission of the pixelWidthChanged signal.
void Worker::setPixelWidth(int value) { if (value != _pixelWidth) { // don't report unnecessary changes _pixelWidth = value; emit pixelWidthChanged(); // this enables: - reevaluation of existing bindings to pixelWidth // - onPixelWidthChanged signal handlers } }
The guard for the value change is useful because it prevents extra work by the QML engine. Each emission of a change signal causes the corresponding READ function to be called, which may lead to needless reevaluation of bindings.
For properties declared in QML, the engine handles sending signals and filtering no-op writes. In C++, the developer has more control but also more responsibility.
-
I tried to emit the signal within the Worker::setPixelWidth() function
I tried again following your suggestion but still no luck
-
Bit of a topic jump, but if I understand correctly, the current issue is that Creator isn't offering autocompletion for the onPixelWidthChanged signal handler.
From the screenshot, it looks like the handler is being defined in a TextField, not a Worker. That doesn't work because Creator and the QML engine look for a matching signal from the scope of the object containing the handler. Two easy options for handling a signal from another object are:
Connections QML type:
Worker { id: worker } TextField { id: textfield } Connections { target: worker onPixelWidthChanged: textfield.text = "signal handled" }
Propagate from the source object:
Worker { onPixelWidthChanged: textfield.text = "signal handled" } TextField { id: textfield }
-
Ok. This makes sense... but nope !
The simple goal here is :
- i have multiple choices (video format) : radio buttons
- i have two textfields : one for the width and one for the height of the image in pixels
- if selected video format is 16/9 : if i change the image width, the height is automatically calculated. I i change the height, the width is calculated.
All this made me learn a lot about QML but i still miss something.
I can't even make a textfield display a value according to what is entered in another textfield...
-
Is the question at this point how to get one text input to depend on another and the reverse (without a binding loop)?
This demonstrates two text inputs, top and bottom. Type into top, click on bottom, and the text in bottom is changed. There are other mechanisms for deciding when to update the non-selected text input. onEditingFinished happens to make for a simple if imperfect example.
import QtQuick 2.3 import QtQuick.Window 2.2 Window { visible: true TextInput { id: top width: parent.width height: parent.height / 2 - 1 onEditingFinished: bottom.text = "from top: " + text } Rectangle { height: 1 width: parent.width anchors.centerIn: parent color: "black" } TextInput { id: bottom width: parent.width height: parent.height / 2 - 1 anchors.bottom: parent.bottom onEditingFinished: top.text = "from bottom: " + text } }
-
hello,
yes this works. It already did that way. But where i can't get it to work is when using c++ bindings
https://gist.github.com/martialgallorini/66a486aea1f4ce884dc4
-
Once the basic mechanisms of type registration and get/set/notify functions for a property are working, Qml versus C++ should not matter.
Have you tried producing a smaller example, eliminating everything unrelated to the issue? The process may make the issue obvious to you. It would certainly help me. I'm guessing that your expectation is that this line will result in param.pixelHeight being updated:
main.qml line 41: onEditingFinished: param.calcPixelHeight()
What I'm missing is an attempt to change the value of param.pixelWidth. Editing the pixelWidth TextField will result in param.pixelHeight be recalculated with the default param.pixelWidth, not the content of the TextField.
-
As a general rule, please post code examples to the forum, or at least to somewhere unlikely to change or disappear. It helps keep threads useful for future readers.
The Worker pixelWidth property is uninitialized and the UI doesn't provide a way to set it. Triggering the pWidth onEditingFinished handler has undefined behavior in the process of updating pixelHeight. Presumably the goal is to have the TextInputs display pixelWidth and pixelHeight, in which case the text properties of each input should be bound to the respective worker properties. Changing the worker properties can be achieved by setting them using onEditingFinished instead of or prior to calling Worker::calcHeight()
-
Hello,
Yes i know, but i find the code presentation on this forum very messy and difficult to read...
Here what i have so far, which is working :
Worker.h
#ifndef WORKER_H #define WORKER_H #include <QObject> class Worker : public QObject { Q_OBJECT Q_PROPERTY(float pixelWidth READ pixelWidth WRITE setPixelWidth NOTIFY pixelWidthChanged) Q_PROPERTY(float pixelHeight READ pixelHeight WRITE setPixelHeight NOTIFY pixelHeightChanged) public: explicit Worker(QObject *parent = 0); float pixelWidth(); void setPixelWidth(float value); float pixelHeight(); void setPixelHeight(float value); Q_INVOKABLE void calcHeight(); signals: void pixelWidthChanged(); void pixelHeightChanged(); public slots: private: float _pixelHeight; float _pixelWidth; }; #endif // WORKER_H
Worker.cpp
#include "worker.h" Worker::Worker(QObject *parent) : QObject(parent) { //_pixelWidth = 1920; } float Worker::pixelWidth() { return _pixelWidth; } void Worker::setPixelWidth(float value) { _pixelWidth = value; emit pixelWidthChanged(); } float Worker::pixelHeight() { return _pixelHeight; } void Worker::setPixelHeight(float value) { _pixelHeight = value; emit pixelHeightChanged(); } void Worker::calcHeight() { _pixelHeight = (_pixelWidth * 9) / 16; emit pixelHeightChanged(); }
main.cpp
#include <QApplication> #include <QQmlApplicationEngine> #include <QtQml> #include "worker.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); qmlRegisterType<Worker>("Worker.interface", 1, 0, "Worker"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
main.qml
import QtQuick 2.3 import QtQuick.Controls 1.2 import Worker.interface 1.0 ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("Hello World") Worker { id: worker } menuBar: MenuBar { Menu { title: qsTr("File") MenuItem { text: qsTr("&Open") onTriggered: console.log("Open action triggered"); } MenuItem { text: qsTr("Exit") onTriggered: Qt.quit(); } } } TextInput { id: pWidth x: 210 y: 118 width: 230 height: 43 text: worker.pixelWidth font.pixelSize: 21 onEditingFinished: { worker.pixelWidth = text worker.calcHeight() } } TextInput { id: pHeight x: 210 y: 193 width: 230 height: 47 text: worker.pixelHeight font.pixelSize: 21 } }
thanks a lot for your support
-
It looks like the basic mechanics of the problem have been worked out. Using C++ property READ and NOTIFY functions from Qml is demonstrated via binding pHeight.text to worker.pixelHeight. main.qml can be reformulated to use the pixelWidth WRITE with minor changes. Here's a quick modification:
import QtQuick 2.3 import QtQuick.Window 2.0 import Worker.interface 1.0 Window { visible: true width: 640 height: 480 Worker { id: worker pixelWidth: Number(pWidth.text) // prevent errors from empty or non-numeric input onPixelWidthChanged: calcHeight() } TextInput { id: pWidth x: 210 y: 118 width: 230 height: 43 text: "1920" // Initial value } TextInput { id: pHeight x: 210 y: 193 width: 230 height: 47 text: worker.pixelHeight } }