Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML Window min-width

Tags:

text

qt

qt4

qml

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();
}
like image 736
Stefano Avatar asked Sep 17 '25 18:09

Stefano


2 Answers

view.setMinimumSize(QSize(min-width,min-height));
like image 90
ScarCode Avatar answered Sep 23 '25 10:09

ScarCode


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.

like image 41
W.B. Avatar answered Sep 23 '25 11:09

W.B.