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. Qml class doesn't work
Qt 6.11 is out! See what's new in the release blog

Qml class doesn't work

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 521 Views 1 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.
  • R Offline
    R Offline
    realroot
    wrote on last edited by realroot
    #1

    In Qt Creator I am trying to use a custom qml class I can add objects but I get this error:

    Invalid property name 'x' (M16)
    

    whenever I set a property.
    And nothing is displayed when I launch the app.

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        qmlRegisterType(QUrl("qrc:/MyTimer.qml"), "MyTimers", 1, 0, "MyTimer");
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    
    main.qml
    
    import QtQuick
    import QtQuick.Window
    import QtMultimedia
    import QtQuick.Controls
    import QtQuick.Layouts
    import MyTimers
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Timers")
        color: "#000000"
    
            MyTimer {
                id: timer1
                countdownValue: 30
            }
    
    }
    

    MyTimer.qml

    import QtQuick
    import QtQuick.Controls
    
    Item {
        id: root
        property int countdownValue: 30
    
    	Rectangle {
    		anchors.fill: parent
    		color: "red"
                    width: 600
                    height: width
    	}
    }
    

    Since it wasn't working I changed the class to have only a rectangle (that it is not shown when I run it).

    MyTimer.qml and main.qml are specified in resources.qrc.

    B 1 Reply Last reply
    0
    • R Offline
      R Offline
      realroot
      wrote on last edited by
      #5

      I need to remove from main.cpp:

      qmlRegisterType(QUrl("qrc:/MyTimer.qml"), "MyTimers", 1, 0, "MyTimer");
      

      And from main.qml:

      import MyTimers
      
      1 Reply Last reply
      0
      • R realroot

        In Qt Creator I am trying to use a custom qml class I can add objects but I get this error:

        Invalid property name 'x' (M16)
        

        whenever I set a property.
        And nothing is displayed when I launch the app.

        main.cpp

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        
        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
        
            qmlRegisterType(QUrl("qrc:/MyTimer.qml"), "MyTimers", 1, 0, "MyTimer");
        
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        
            return app.exec();
        }
        
        main.qml
        
        import QtQuick
        import QtQuick.Window
        import QtMultimedia
        import QtQuick.Controls
        import QtQuick.Layouts
        import MyTimers
        
        Window {
            width: 640
            height: 480
            visible: true
            title: qsTr("Timers")
            color: "#000000"
        
                MyTimer {
                    id: timer1
                    countdownValue: 30
                }
        
        }
        

        MyTimer.qml

        import QtQuick
        import QtQuick.Controls
        
        Item {
            id: root
            property int countdownValue: 30
        
        	Rectangle {
        		anchors.fill: parent
        		color: "red"
                        width: 600
                        height: width
        	}
        }
        

        Since it wasn't working I changed the class to have only a rectangle (that it is not shown when I run it).

        MyTimer.qml and main.qml are specified in resources.qrc.

        B Offline
        B Offline
        Bob64
        wrote on last edited by
        #2

        @realroot Your MyTimer is defined as an Item which needs to have a size specified in order for it to be shown. I am not sure what will happen with the child Rectangle as you are both filling the parent (whose size is 0) and setting its width and height.

        GrecKoG R 2 Replies Last reply
        0
        • B Bob64

          @realroot Your MyTimer is defined as an Item which needs to have a size specified in order for it to be shown. I am not sure what will happen with the child Rectangle as you are both filling the parent (whose size is 0) and setting its width and height.

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #3

          @Bob64 said in Qml class doesn't work:

          I am not sure what will happen with the child Rectangle as you are both filling the parent (whose size is 0) and setting its width and height.

          anchors.fill has the priority over width and height

          1 Reply Last reply
          1
          • B Bob64

            @realroot Your MyTimer is defined as an Item which needs to have a size specified in order for it to be shown. I am not sure what will happen with the child Rectangle as you are both filling the parent (whose size is 0) and setting its width and height.

            R Offline
            R Offline
            realroot
            wrote on last edited by
            #4

            @Bob64 I added the size:

            Item {
                id: root
                property int countdownValue: 30
                property int countdown : countdownValue
                width: 600
                height: width
            

            But as before nothing is shown.
            And I can't set properties.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              realroot
              wrote on last edited by
              #5

              I need to remove from main.cpp:

              qmlRegisterType(QUrl("qrc:/MyTimer.qml"), "MyTimers", 1, 0, "MyTimer");
              

              And from main.qml:

              import MyTimers
              
              1 Reply Last reply
              0
              • R realroot has marked this topic as solved on

              • Login

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