Results 1 to 5 of 5

Thread: How to catch the width/height changes after the application startup occured in QML?

  1. #1
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default How to catch the width/height changes after the application startup occured in QML?

    From what I have seen both width and height properties are 0 when application starts and 0 when Component.completed signal occurs.

    This is a very strange behavior and makes it difficult to catch the resizes because onWidthChanged triggers after startup aswell.

    My question would be: how others are avoiding this issue?

    Qt 5.6.2

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to catch the width/height changes after the application startup occured in QM

    Quote Originally Posted by adutzu89 View Post
    This is a very strange behavior and makes it difficult to catch the resizes because onWidthChanged triggers after startup aswell.
    Why is this a strange behavior?
    The initial value of a lot of properties is 0 (or the equivalent of the respective type) and any value change triggers the change signal.
    Why would you think that a change from 0 to something else would not trigger the signal?

    Quote Originally Posted by adutzu89 View Post
    My question would be: how others are avoiding this issue?
    In most cases code doesn't react to change signals at all but uses property bindings to use the values directly whereever needed.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to catch the width/height changes after the application startup occured in QM

    Why is this a strange behavior?
    The initial value of a lot of properties is 0 (or the equivalent of the respective type) and any value change triggers the change signal.
    The thing is you might expect the values of properties to have set the values you put when the objects are created, like when class members are initialised in constructors.
    Like in my case because I gave the values when declaring the object and not after that, so I didn't knew the object would have the size properties set to 0 and afterwards it changes to the values I have given.(I didn't found any info on this in the documentation; maybe I missed it?)
    Furthermore Component.onCompleted gave me the ideea that after that object was created it should have the values of properties set, yet the properties where still set to 0.
    I didn't encountered this behavior before in other languages, that is why it's strange.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to catch the width/height changes after the application startup occured in QM

    All values of an object are initialiized by the object's constructor.

    Since the QML engine has to instantiate random QObject the only constructors it can use are the default constructor and the constructor that takes a single QObject pointer (the parent).

    As a logical conclusion we can derive that properties can only be set after object constructions.

    Now, Component.onCompleted is usually triggered when the object and its children have been created and usually includes the initially set property values.
    But it will, for example, only unlikely include the values of any property bindings.

    Completion in the declarative concept means something is there and can be fully used, which is the case even if the object has its initial values.

    As values are usually used via property bindings it also doesn't matter if they change as their updated values will be used whenever and whereever needed.

    Can you give an indication what you are trying to achieve? There might be better options that shoe-horning something into QML that it wasn't designed for.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    adutzu89 (11th November 2016)

  6. #5
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to catch the width/height changes after the application startup occured in QM

    Thank you anda_skoa for your help .

    Now, Component.onCompleted is usually triggered when the object and its children have been created and usually includes the initially set property values.
    But it will, for example, only unlikely include the values of any property bindings.

    Completion in the declarative concept means something is there and can be fully used, which is the case even if the object has its initial values.
    Mixed this with an if..else instead of just if condition and brought me to a "wild goose chase" trying to figure out why is my code not working correctly.
    I succesfully found a simple solution in the same day, like pointed out an if condition instead of if..else.

    For the sake of curiosity I had a Flickable with 2 items, let's call them item1 and item2, item2.anchors.left: item1.right and flickable's contentX: item1.width with a button which toggled through them.
    After the first full toggle (item2 -> item1 and back again), contentX was again item1.width but didn't correctly adjusted when item1.width changed even tough at startup it did, instead it just remained at last item1.width when the toggle occured so I have to update contentX everytime the width changes.
    With Component.onCompleted showing the width of the item1 being 0 didn't helped(instead confusing me) I've resorted to logging every bit of size change and contentX change until I figured it out how to make it work as I wanted.
    Another thing is that the size of the items is binded to the flickable which in turn has it's binded to a rectangle and the rectangle to an ApplicationWindow .

    It's a good thing that it works for the moment. Thank again for all your help.

Similar Threads

  1. how to get image height and width in qt
    By iswaryasenthilkumar in forum Newbie
    Replies: 1
    Last Post: 31st March 2015, 06:52
  2. Pusbutton height and width
    By seany in forum Qt Programming
    Replies: 6
    Last Post: 7th June 2013, 06:35
  3. looking for QDockWidget type, but not take whole width/height
    By Thomas_Lerman in forum Qt Programming
    Replies: 0
    Last Post: 27th October 2011, 20:09
  4. Widget Height and Width
    By in_dbasu in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2011, 08:44
  5. width and height of QTabWidget
    By chikkireddi in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 13:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.