I'm creating a window with QML and the code below. How can I set a min-width for the window so that I can't resize less than the values I define?
Rectangle {
color: red;
width: 300
height: 100
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("QML/main.qml"));
view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
view.show();
return app.exec();
}
view.setMinimumSize(QSize(min-width,min-height));
I don't know, which version of QML you're using, but if you can youse QtQuick 2, then you would set ApplicationWindow
as your toplevel Item in QML like this:
import QtQuick 2.0
ApplicationWindow {
id: appWnd
minimumWidth: 300
minimumHeight: 300
}
You can also have a look at implicitWidth
and implicitHeight
properties for each particular QML type and set appWnd
minimumWidth/Height to be implicitWidth/Height of containing layout. For example:
import QtQuick 2.0
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.0
ApplicationWindow
{
minimumWidth: gridLayout.implicitWidth
minimumHeight: gridLayout.implicitHeight
height: 500
width: 500
color: "gold"
GridLayout {
id: gridLayout
anchors.centerIn: parent
columns: 2
Button {text: "Push me" }
Button {text: "Push me" }
Button {text: "Push me" }
Button {text: "Push me" }
Button {text: "Push me" }
Button {text: "Push me" }
}
}
This will not let the app window scale smaller than the controls it contains (as implicit layout's width and height is equal to the sum of contained items implicit widths and heights).
You can also bind to the absolute minimum width of the layout by using gridLayout.Layout.minimumWidth
, as described in the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With