Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QtQuick and C++ or QML + Javascript ?
Qt 6.11 is out! See what's new in the release blog

QtQuick and C++ or QML + Javascript ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
21 Posts 3 Posters 18.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marty
    wrote on last edited by
    #12

    I tried to emit the signal within the Worker::setPixelWidth() function

    I tried again following your suggestion but still no luck

    http://postimg.org/image/9z8rtkwin/

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #13

      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
      }
      

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Marty
        wrote on last edited by
        #14

        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...

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #15

          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
              }
          }
          

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Marty
            wrote on last edited by Marty
            #16

            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

            1 Reply Last reply
            0
            • jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #17

              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.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Marty
                wrote on last edited by
                #18

                Ok i updated the code with a simpler example.
                Where am i wrong ?

                thanks a lot

                1 Reply Last reply
                0
                • jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #19

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

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Marty
                    wrote on last edited by
                    #20

                    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

                    1 Reply Last reply
                    0
                    • jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #21

                      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
                          }
                      }
                      

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved