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. [SOLVED] "Width" property = 0 in onCompleted handler

[SOLVED] "Width" property = 0 in onCompleted handler

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.7k 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.
  • Z Offline
    Z Offline
    zword
    wrote on last edited by
    #1

    Hi!

    What i'm trying to achieve is to dynamically populate Column with the following:

    Child.qml:
    @
    Column {
    property string _text
    property int _width

    Text {
            id: note
            text: _text
            width: parent.width
            //width: _width
            wrapMode: Text.WordWrap  // <======
    }
    
    Text {
            id: text1
            ...
    }
    
    Component.onCompleted: console.log("width = " + width + " _width = " + _width)
    

    }@

    And i need text's wrapMode:Text.WordWrap. Documentatios says if one wants text to be wrappable it's width must be set explicitly.

    In C++ code i set @view.setResizeMode(QQuickView::SizeRootObjectToView);
    @

    And the main.qml:
    @ Rectangle {
    id: main_rec
    height:rec.height

        //width: 400;                <=======
    
        color: "indianred"
    
        Rectangle {
            id : rec
            width: parent.width
            height:col.height
            x: parent.x
            y: parent.y
    
            border.color: "black"
            border.width: 3
            radius: 5
    
            color: "lightgreen"
    
            Component.onCompleted: console.log("rec:width = " + width, "implicitWidth = " + implicitWidth)
    
            Column {
               x: rec.x
               y: rec.y
    
                id: col
    
               Child { _width: rec.width; idx: 3; _text: "2 12345678910 1112131415 46574839457857" }     //   <==== STATIC, "width" is fine
    
                function populate_col()
                {
                    var component = Qt.createComponent("Child.qml");
                    if (component.status == Component.Ready) {
                        for(var i=0; i<3; i++){
                            console.log("###createComponent #" + i)
                            component.createObject(col, {"width": rec.width, "_width": rec.width,
                                                       "_text" : i + " Very very very very looooooooong text"}) // <====== "width" = 0
                        }
                    }
    
                    if (component.status == Component.Error) {
                        console.log("###Error creating component: " + component.errorString())
                    }
                }
    
                Component.onCompleted: populate_col()
            }
        }
    }@
    

    The problem is when i don't explicitly set root's item width and create component dynamically and pass "width" property to Text it is equal 0.
    But when i create it statically "width" has correct value.

    I think i missing some basics.
    Could you point me where i'm wrong?

    Thanx

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dasRicardo
      wrote on last edited by
      #2

      Hello,

      the problem is onComplete is more like a init finished slot and doesn't indicates if the element is complete rendered. Take a look at "this":http://qt-project.org/doc/qt-4.8/qml-component.html#onCompleted-signal. I have the same problem often but doesn't find an event or anything else that emit if the element rendering / dimension calculating is finished. What i do is to use the property changing in your case onWidthChanged.

      **Sorry for my english :)

      PLEASE ADD [SOLVED] TO YOUR THREAD TITLE IF IT&#x27;S SOLVED.**

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zword
        wrote on last edited by
        #3

        Thank you sir, that helped!

        But than followed another problem:

        When i change from portrait to landscape mode i cannot destroy object in onWidthChanged handler in a right way. It seems that object is actually destroyed but it's still visiable. I have a link in this object and after object is destroyed link is still visiable and associated OnLinkActivated handler says TypeError: Cannot read property 'index' of undefined.

        Day of googling gave me nothing.

        Thanx again!

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dasRicardo
          wrote on last edited by
          #4

          Sorry but I have no clue what your program did and why u need to destroy objects when u switch from portrait to landscape mode. Please add [SOLVED] to the thread title because the main thread problem is solved i think?

          **Sorry for my english :)

          PLEASE ADD [SOLVED] TO YOUR THREAD TITLE IF IT&#x27;S SOLVED.**

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zword
            wrote on last edited by
            #5

            bq. why u need to destroy objects when u switch from portrait to landscape mode

            With this sentence you gave me the clue and i solved my problem.

            But, anyway, just for fun, why object is still visible when i destroy it in onWidthChanged handler?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dasRicardo
              wrote on last edited by
              #6

              Hmmm, i would say that the painting is always done when the handler react and if you destroy an object u need to force redrawing. I have no idea how u can force redrawing in qml.

              **Sorry for my english :)

              PLEASE ADD [SOLVED] TO YOUR THREAD TITLE IF IT&#x27;S SOLVED.**

              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