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 use object type properties
Forum Updated to NodeBB v4.3 + New Features

How to use object type properties

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.1k 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
    maydin
    wrote on last edited by maydin
    #1

    I am designing my application structure like below. But I cannot use Button type properties shown below.

    I tried to put them there with Loader but it takes source (QUrl) and sourceComponent (QQmlComponent).

    When I try to bind them to loader source I get this error: Unable to assign Button_QMLTYPE_4 to QUrl

    I searched it a lot but I couldnt make it happen. Or i dont know how to search at all :)

    // main.qml
    
    Page {
        CustomToolbar {
             leftButton: Button {/*properties*/}
             rightButton: Button {/*properties*/}
        }
    }
    
    // CustomToolbar.qml
    Toolbar {
        property Button leftButton;
        property Button rightButton;
    
        // Here should be left button
    
        Title {...}
    
        // Here should be right button
    }
    
    raven-worxR Pablo J. RoginaP 2 Replies Last reply
    0
    • M maydin

      I am designing my application structure like below. But I cannot use Button type properties shown below.

      I tried to put them there with Loader but it takes source (QUrl) and sourceComponent (QQmlComponent).

      When I try to bind them to loader source I get this error: Unable to assign Button_QMLTYPE_4 to QUrl

      I searched it a lot but I couldnt make it happen. Or i dont know how to search at all :)

      // main.qml
      
      Page {
          CustomToolbar {
               leftButton: Button {/*properties*/}
               rightButton: Button {/*properties*/}
          }
      }
      
      // CustomToolbar.qml
      Toolbar {
          property Button leftButton;
          property Button rightButton;
      
          // Here should be left button
      
          Title {...}
      
          // Here should be right button
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @maydin
      how dynamic should your CustomToolbar be in the end?
      Is it enough for you just to set the Buttons in the very beginning? Or do they get replaced dynamically at some point in your application?

      For the first case (completely untested though):

      // CustomToolbar.qml
      Toolbar {
           id: tb
          property Component leftButton: null
          property Component rightButton: null
      
          Title {
               id: title
               anchors.horizontalCenter: parent.horizontalCenter
               ...
          }
      
          Component.onCompleted: {
                    // create left button
                   if( leftButton )
                           leftButton.createObject(tb, {"anchors": {left: tb.left});
                    // create right button
                    if( rightButton )
                           rightButton.createObject(tb, {"anchors": {right: tb.right});
          }
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply
      0
      • M maydin

        I am designing my application structure like below. But I cannot use Button type properties shown below.

        I tried to put them there with Loader but it takes source (QUrl) and sourceComponent (QQmlComponent).

        When I try to bind them to loader source I get this error: Unable to assign Button_QMLTYPE_4 to QUrl

        I searched it a lot but I couldnt make it happen. Or i dont know how to search at all :)

        // main.qml
        
        Page {
            CustomToolbar {
                 leftButton: Button {/*properties*/}
                 rightButton: Button {/*properties*/}
            }
        }
        
        // CustomToolbar.qml
        Toolbar {
            property Button leftButton;
            property Button rightButton;
        
            // Here should be left button
        
            Title {...}
        
            // Here should be right button
        }
        
        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @maydin in addition to @raven-worx proposal, you may want to take a look at QML Camera example, and check how they (re)using the buttons on the right...

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @maydin
          how dynamic should your CustomToolbar be in the end?
          Is it enough for you just to set the Buttons in the very beginning? Or do they get replaced dynamically at some point in your application?

          For the first case (completely untested though):

          // CustomToolbar.qml
          Toolbar {
               id: tb
              property Component leftButton: null
              property Component rightButton: null
          
              Title {
                   id: title
                   anchors.horizontalCenter: parent.horizontalCenter
                   ...
              }
          
              Component.onCompleted: {
                        // create left button
                       if( leftButton )
                               leftButton.createObject(tb, {"anchors": {left: tb.left});
                        // create right button
                        if( rightButton )
                               rightButton.createObject(tb, {"anchors": {right: tb.right});
              }
          }
          
          M Offline
          M Offline
          maydin
          wrote on last edited by
          #4

          @raven-worx It is enough to set Buttons in the very beginning. But this method allows both I guess.

          I realized I need to put title and buttons inside RowLayout in Toolbar. Thats why I wrapped Title inside component and added them to RowLayout dynamically as you show.

          But there may be one more solution. If property is Button instead Component, I can add them to children property of RowLayout with children.push(leftButton) etc.

          Which way is more convenient and why I can define property type as Button, how and where can I use it in future?

          raven-worxR 1 Reply Last reply
          0
          • M maydin

            @raven-worx It is enough to set Buttons in the very beginning. But this method allows both I guess.

            I realized I need to put title and buttons inside RowLayout in Toolbar. Thats why I wrapped Title inside component and added them to RowLayout dynamically as you show.

            But there may be one more solution. If property is Button instead Component, I can add them to children property of RowLayout with children.push(leftButton) etc.

            Which way is more convenient and why I can define property type as Button, how and where can I use it in future?

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @maydin
            Yes my example uses Components and creates them when the ToolBar is about to be created.
            You can also use instances of items (Button) directly as you said.

            Which way is more convenient and why I can define property type as Button, how and where can I use it in future?

            i dont understand that question.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            M 1 Reply Last reply
            0
            • raven-worxR raven-worx

              @maydin
              Yes my example uses Components and creates them when the ToolBar is about to be created.
              You can also use instances of items (Button) directly as you said.

              Which way is more convenient and why I can define property type as Button, how and where can I use it in future?

              i dont understand that question.

              M Offline
              M Offline
              maydin
              wrote on last edited by
              #6

              @raven-worx

              property Button leftButton
              toolbar.children.push(leftButton)
              

              or

              property Component leftButton
              Component.onCompleted: leftButton.createObject(toolbar)
              

              Which way is better to use? What are advantages or disadvantages in different situations.

              raven-worxR 1 Reply Last reply
              0
              • M maydin

                @raven-worx

                property Button leftButton
                toolbar.children.push(leftButton)
                

                or

                property Component leftButton
                Component.onCompleted: leftButton.createObject(toolbar)
                

                Which way is better to use? What are advantages or disadvantages in different situations.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @maydin
                I think for your case it doesn't make much of a difference.

                Components are used when you want to control the time it will be instantiated or simply use them as a template and reuse them whenever you want.
                Items (like when you declare the type of the property to 'Button') are already instantiated when they get assigned.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                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