Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loader size dependent on source

Tags:

qt

qml

qtquick2

I'm working on a little system of message dialog windows in QML. For this I'm using a container with a Loader to load the different messages (these are not just text but text and graphical symbols layouted hence loading a QML file for each individual message). By default these message windows have the same size, so I have my size information directly in the container. But some messages can be longer, therefore I'm looking for a way to use the height of my loaded component if it exceeds the default value. The way I see it my problem can be split into three parts:

  • How to have a container sized by the dimensions of its children?
  • How to access the size information of the loaded component through my Loader object?
  • How to selectively use the larger size?

Any suggestions?

like image 314
DenverCoder21 Avatar asked Dec 05 '25 17:12

DenverCoder21


2 Answers

You can access loaded object using item keyword. Example if your loader id is idLoader, then the created item is idLoader.item, you have 2 solutions to do what you want:

1:How to access the size information of the loaded component through my loader object?

Loader{
id:idLoader
width: (item !== null && typeof(item)!== 'undefined')? item.width : 0
height: (item !== null && typeof(item)!== 'undefined')? item.height: 0
}

2: How to have a container sized by the dimensions of its children? & How to selectivly use the larger size?

  Loader{
        id:idLoader
        width: childrenRect.width
        height : childrenRect.height
        }
like image 173
Mido Avatar answered Dec 08 '25 05:12

Mido


Here's an idea, untested. Take the max of the child's preferred height or a hard-coded minimum, whichever is larger.

Loader {
   height: Math.max(item ? item.implicitHeight : 0, 200)
}
like image 34
Chris Dolan Avatar answered Dec 08 '25 07:12

Chris Dolan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!