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. How to properly set a property of a Qml item via a QQuickView instance ?
QtWS25 Last Chance

How to properly set a property of a Qml item via a QQuickView instance ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 865 Views
  • 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.
  • L Offline
    L Offline
    LRDPRDX
    wrote on 22 Dec 2022, 06:00 last edited by
    #1

    Hi, Qt'ers !

    I have three files:

    • BDialog.qml - item definition

    • BEditorDialog.qml - a BDialog with a buttons and checkbox

    • main.cpp - C++

    The content is the following:

    // main.cpp
    #include <QApplication>
    #include <QQuickView>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    
    class Controller : public QObject
    {
        Q_OBJECT
    
        public slots:
            bool getStatus() { return true; }
    };
    
    int main( int argc, char **argv )
    {
        QApplication app( argc, argv );
    
        Controller controller;
    
        QQuickView view;
        view.setSource( QUrl::fromLocalFile( "qrc:/BEditorDialog.qml" ) );
    
        view.rootContext()->setContextProperty( "controller", QVariant::fromValue( &controller ) );
        view.rootContext()->setContextProperty( "buttonText", "Text" );
    
        view.show();
        return app.exec();
    }
    
    #include "main.moc"
    
    //BDialog.qml
    
    import QtQuick 2.6
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    
    Item {
        property QtObject controller
        property string buttonText
    }
    
    //BEditorDialog.qml
    
    import QtQuick 2.6
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    
    BDialog {
        id: root
    
        RowLayout {
            CheckBox {
                text: "Check status"
                checked: root.controller.getStatus()
            }
    
            Button {
                text: root.buttonText
            }
        }
    }
    

    I compile and run this but the combobox isn't checked and there is no text on the button, also I have (obviously) got the error message:

    qrc:/BEditorDialog.qml:11: TypeError: Cannot call method 'getStatus' of null
    

    The problem disappears if I remove the root. prefix in the BEditorDialog.qml: root.controller -> controller together with the removing of property declaration from the BDialog.qml but I want to set a property of a "base" qml -- and not create a new one.

    So could you give me a direction, I've read some documentation but haven't found a solution. I think this should be done via the setInitialProperties member function but I have only Qt 5.12.

    Thank you in advance.


    Platform: linux
    Qt: 5.12

    D 1 Reply Last reply 22 Dec 2022, 06:53
    0
    • L Offline
      L Offline
      LRDPRDX
      wrote on 22 Dec 2022, 12:09 last edited by
      #3

      Here is the answer.

      1 Reply Last reply
      0
      • L LRDPRDX
        22 Dec 2022, 06:00

        Hi, Qt'ers !

        I have three files:

        • BDialog.qml - item definition

        • BEditorDialog.qml - a BDialog with a buttons and checkbox

        • main.cpp - C++

        The content is the following:

        // main.cpp
        #include <QApplication>
        #include <QQuickView>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        
        
        class Controller : public QObject
        {
            Q_OBJECT
        
            public slots:
                bool getStatus() { return true; }
        };
        
        int main( int argc, char **argv )
        {
            QApplication app( argc, argv );
        
            Controller controller;
        
            QQuickView view;
            view.setSource( QUrl::fromLocalFile( "qrc:/BEditorDialog.qml" ) );
        
            view.rootContext()->setContextProperty( "controller", QVariant::fromValue( &controller ) );
            view.rootContext()->setContextProperty( "buttonText", "Text" );
        
            view.show();
            return app.exec();
        }
        
        #include "main.moc"
        
        //BDialog.qml
        
        import QtQuick 2.6
        import QtQuick.Controls 2.0
        import QtQuick.Layouts 1.3
        
        Item {
            property QtObject controller
            property string buttonText
        }
        
        //BEditorDialog.qml
        
        import QtQuick 2.6
        import QtQuick.Controls 2.0
        import QtQuick.Layouts 1.3
        
        BDialog {
            id: root
        
            RowLayout {
                CheckBox {
                    text: "Check status"
                    checked: root.controller.getStatus()
                }
        
                Button {
                    text: root.buttonText
                }
            }
        }
        

        I compile and run this but the combobox isn't checked and there is no text on the button, also I have (obviously) got the error message:

        qrc:/BEditorDialog.qml:11: TypeError: Cannot call method 'getStatus' of null
        

        The problem disappears if I remove the root. prefix in the BEditorDialog.qml: root.controller -> controller together with the removing of property declaration from the BDialog.qml but I want to set a property of a "base" qml -- and not create a new one.

        So could you give me a direction, I've read some documentation but haven't found a solution. I think this should be done via the setInitialProperties member function but I have only Qt 5.12.

        Thank you in advance.


        Platform: linux
        Qt: 5.12

        D Offline
        D Offline
        Drooke
        wrote on 22 Dec 2022, 06:53 last edited by
        #2

        @LRDPRDX

        You haven't set the property for the loaded qml file.

        You may be able to se it like this:

        itemLoader.setSource("Test.qml", {"stringa1": "ScrivoStr1", "stringa2": "ScrivoStr2"})

        Or in the parent qml set the property to the context property you configured in the backend.

        Property QObject myObject: myObject

        1 Reply Last reply
        0
        • L Offline
          L Offline
          LRDPRDX
          wrote on 22 Dec 2022, 12:09 last edited by
          #3

          Here is the answer.

          1 Reply Last reply
          0

          2/3

          22 Dec 2022, 06:53

          • Login

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