Reputation: 1544
I'm using a QDeclarativeView
to display a QML widget. How do I set the minimum height of the QDeclarativeView
/widget, so it doesn't get smaller then I want? I'd like to be able to bind it to the minimum size of child widgets, so widgets don't overlap and everything stays properly spaced.
Upvotes: 4
Views: 8031
Reputation: 809
iBelevie's solution is solid, but a little bit of an overkill. QML is to some extent CSS on steroids and supports inline javascript. I hope you now know where this is going :)
Simply define your property as a conditional statement:
height: (root.height < threshold) ? minHeight: WhateverIsEpressibleInJavaScript
. It is not slick and solid solution, but at least it is a one-liner inside markup. I leave it for the reader to contemplate whether it's good or not.
Upvotes: 4
Reputation: 1544
First, in your main widget, create two properties to hold the the minimum width and height:
property int minWidth: <whatever>
property int minHeight: <whatever>
If you want it based the minimum size of the widget's children, you could do something like this:
Item {
id: root
property int minWidth: child.minWidth + 40; // Adds a 40px margin
property int minHeight: child.minHeight + 40; // Adds a 40px margin
Item {
id: child
property int minWidth: <whatever>
property int minHeight: <whatever>
anchors.centerIn: root
}
}
QDeclarativeView
Then, in the class that creates the QDeclarativeView
, define two slots (view
is the QDeclarativeView
):
void onMinimumWidthChanged() {
view->setMinimumWidth(view->rootObject()->property("minWidth").toInt());
}
void onMinimumHeightChanged() {
view->setMinimumHeight(view->rootObject()->property("minHeight").toInt());
}
Then, when you create the QDeclarativeView
:
QDeclarativeView *view = new QDeclarativeView(this);
view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
view->setSource(/* Whatever */);
QDeclarativeProperty(view->rootObject(), "minWidth").connectNotifySignal(this, SLOT(onMinimumWidthChanged()));
QDeclarativeProperty(view->rootObject(), "minHeight").connectNotifySignal(this, SLOT(onMinimumHeightChanged()));
onMinimumWidthChanged();
onMinimumHeightChanged();
Now, the minimum size of the QDeclarativeView
will be bound to the minimum size as defined in the main QML widget. If you change the minimum size anywhere in QML, the minimum size of the QDeclarativeView
will change as well.
Upvotes: 3