Qt World Summit: Register Today!

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

  • Hi!

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

    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

        //width: 400;                <=======
        color: "indianred"
        Rectangle {
            id : rec
            width: parent.width
            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?


  • 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.

  • 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!

  • 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?

  • 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?

  • 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.

Log in to reply