1
votes

This seems a simple question but I can't find a solution. I have a ScrollView in QML, and it's layout police is configured by anchors, no fixed size specified at all. So when users resize the window it automatically stretch. In some of my classes, I need to know the scroll view size information, so I inherit those classes from QObject and register them as QML type, then using QProperty to bind to ScrollView witdh and height property.

The result is QML engine seems to adjust the ScrollView several times before it's fixed. And some of these adjustment change width to some negative values like -140. In the end it changes it to a positive value but the value is different from what Design calculates the size. For example, in my case, the last call to set height sets it to 290, but if I go to Design and check the ScrollView size, its height is 310.

What is the proper way to get the correct size of a view from QML in C++ classes?

Main QML files

ApplicationWindow {
    id : applicationWindow
    visible: true
    width: 640
    height: 480
    minimumWidth: 640
    minimumHeight: 480

    // skipped some unrelated opponents

    StackView {
            id: stackView
            anchors.fill: parent
            focus: true

            initialItem: {
                [{item : Qt.resolvedUrl("AboutPage.qml")},
                 {item : Qt.resolvedUrl("ConfigurationPage.qml")}]
            }

    }
}

ConfigurationPage.qml

Rectangle {
    ScreenManager {
        id: screenManager
        screenModel: screenModel
        managementViewW: screenArrangementScrollView.width
        managementViewH: screenArrangementScrollView.height
    }
    ScrollView {
            id: screenArrangementScrollView

            frameVisible: true
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.topMargin: 100
            anchors.bottomMargin: 70
            anchors.leftMargin: 70
            anchors.horizontalCenter: parent.horizontalCenter
    }
}

ScreenManager

class ScreenManager : public QObject
{
    Q_OBJECT

public:
    ScreenManager();
    ~ScreenManager();

    Q_PROPERTY(ScreenModel* screenModel READ screenModel WRITE setScreenModel)
    Q_PROPERTY(int managementViewW READ managementViewW WRITE setManagementViewW)
    Q_PROPERTY(int managementViewH READ managementViewH WRITE setManagementViewH)
};
1

1 Answers

0
votes

The 20 pixels difference come from the menu bar.