Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Component template

Component template

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 778 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.
  • A Offline
    A Offline
    alecs26
    wrote on last edited by
    #1

    Hello,

    I want to define a custom component and to instanciate it in my main QML file. I use QML Controls2.
    I read the Qt tutorial and the forum but there is something I don't get. It seems to be a basic problem but it eludes me.

    Here is the content of buttonal1.qml

    import QtQuick 2.0
    
    Rectangle
    {
      width: 300
      height: 200
      color:"#0f0f0f"
      property alias nameTextTop: nameText // This alias is defined at top level and bound to Text element
      Text
      {
        id: nameText
        color: "blue"
        text: "Some text fragment"
      }
    }
    

    and here is the content of main.qml:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
       buttonal1
       {
           width: 200
       }
    
    }
    

    However, I get an error in my main file (buttonal1 is not recognized "invalid property name").

    Both main.qml and buttonal1.qml are under Resources/qml.qrc/

    Thank you so much,

    Alex

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alecs26
      wrote on last edited by
      #2

      Ok, I found the answer. My template file "buttonal1.qml" was beginning with a lower case letter and it is why it didn't work....It must start with an upper case letter.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        att_
        wrote on last edited by
        #3

        Hi,

        I went through the exact same problem ... changed the component beginning letter to upper case. Now I get this run time error "qrc:/main.qml:21 MButton is not a type". Please tell me what is wrong?

        Anthony

        MButton.qml
        import QtQuick 2.0
        Rectangle
        {
        width: 100; height: 30; color: "yellow"
        }

        main.qml
        import QtQuick 2.7
        import QtQuick.Controls 2.0

        ApplicationWindow
        {
        visible: true
        width: 640
        height: 480
        title: qsTr("Calculator")

        Column
        {
            width: parent.width
            Rectangle
            {
                width: 100
                height: 100
                color: "red"
            }
            MButton
            {
            }
        }
        

        }

        qml.qrc
        <RCC>
        <qresource prefix="/">
        <file>main.qml</file>
        <file>content/MButton.qml</file>
        </qresource>
        </RCC>

        G 1 Reply Last reply
        0
        • A att_

          Hi,

          I went through the exact same problem ... changed the component beginning letter to upper case. Now I get this run time error "qrc:/main.qml:21 MButton is not a type". Please tell me what is wrong?

          Anthony

          MButton.qml
          import QtQuick 2.0
          Rectangle
          {
          width: 100; height: 30; color: "yellow"
          }

          main.qml
          import QtQuick 2.7
          import QtQuick.Controls 2.0

          ApplicationWindow
          {
          visible: true
          width: 640
          height: 480
          title: qsTr("Calculator")

          Column
          {
              width: parent.width
              Rectangle
              {
                  width: 100
                  height: 100
                  color: "red"
              }
              MButton
              {
              }
          }
          

          }

          qml.qrc
          <RCC>
          <qresource prefix="/">
          <file>main.qml</file>
          <file>content/MButton.qml</file>
          </qresource>
          </RCC>

          G Offline
          G Offline
          gd59
          wrote on last edited by
          #4

          @att_
          Hi,
          Did you ever find out what was wrong? I'm starting using Qt right now, and running into the same line if problems, Qt not wanting to use some objects I have refactored to move them in a different file...

          Thanks.

          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