如何将位于 StackView 中的 ScrollView 居中?

huangapple go评论66阅读模式
英文:

How to center ScrollView that is in StackView?

问题

我无法将推送的ScrollView居中到StackView中。我尝试了很多方法,但都不起作用。你能帮我解决这个问题吗?

//Main.qml
...
StackView {
    id: stackView
    anchors.fill: parent
    initialItem: mainView
}
Loader {
    id: serialportLoader
    active: false
    source: "secondFile.qml"
}
...
//通过按下一个按钮将"secondFile.qml"推送
Action {
    id: openSerialportSettings
    text: "Open serial port"
    onTriggered: {
        serialportLoader.active = true;
        stackView.push(serialportLoader.item);
    }
}
...
//secondFile.qml
ScrollView   {
    ColumnLayout {
        spacing: 5
        anchors.centerIn: parent
        Rectangle {
            color: "green"
            height: 50
            Layout.fillWidth: true
        }
...

如果我向ScrollView添加一个空的Item,它会居中,但无法滚动:

ScrollView   {
    Item {
    }
    ColumnLayout {
...

我猜我可能是在错误地使用ScrollView,因为我用ScrollView和Page代替它时得到这些结果:

ScrollView/*Page*/  {
    id: topItem
    ColumnLayout {
        spacing: 5
        anchors.centerIn: parent
        Rectangle {
            color: "green"
            height: 200
            width: 300
            Layout.fillWidth: true
        }
    }
}

如何将位于 StackView 中的 ScrollView 居中?

如何将位于 StackView 中的 ScrollView 居中?

如何将位于 StackView 中的 ScrollView 居中?


[1]: https://i.stack.imgur.com/z7ndt.png
[2]: https://i.stack.imgur.com/VlQRg.png
[3]: https://i.stack.imgur.com/bzOgY.png
英文:

I can't center pushed ScrollView into the StackView. I've tried a bunch of ways, but nothing works. Can you help me with this, please?

//Main.qml
...
StackView {
    id: stackView
    anchors.fill: parent
    initialItem: mainView
}
Loader {
    id: serialportLoader
    active: false
    source: "secondFile.qml"
}
...
//Push "secondFile.qml" by pressing a button with the action:
Action {
    id: openSerialportSettings
    text: "Open serial port"
    onTriggered: {
        serialportLoader.active = true;
        stackView.push(serialportLoader.item);
    }
}
...
//secondFile.qml
ScrollView   {
    ColumnLayout {
        spacing: 5
//        Layout.alignment: Qt.AlignVCenter
        anchors.centerIn: parent
        Rectangle {
            color: "green"
            height: 50
            Layout.fillWidth: true
        }
...

如何将位于 StackView 中的 ScrollView 居中?
If I add an empty Item to the ScrollView, it comes to the center but no longer scrollable:

ScrollView   {
    Item {
    }
    ColumnLayout {
...

如何将位于 StackView 中的 ScrollView 居中?

I guess I'm just using ScrollView wrong, because I get these results with ScrollView and Page instead of it:

ScrollView/*Page*/  {
    id: topItem
    ColumnLayout {
        spacing: 5
        anchors.centerIn: parent
        Rectangle {
            color: "green"
            height: 200
            width: 300
            Layout.fillWidth: true
        }
    }
}

如何将位于 StackView 中的 ScrollView 居中?

In Online editor

答案1

得分: 1

正如JarMan所暗示的,问题/混淆的原因是你的Loader实现使得你的StackView实现变得模糊不清。我已经创建了一个版本的你的应用程序,只使用了StackView,并让它接收"SecondPage.qml":

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
    header: AppToolBar { }
    StackView {
        id: stackView
        anchors.fill: parent
        initialItem: "MainView.qml"
    }
}

// MainView.qml
import QtQuick
import QtQuick.Controls
Page {
    Label {
        anchors.centerIn: parent
        text: "MainView.qml"
    }
    Button {
        anchors.horizontalCenter: parent.horizontalCenter
        y: parent.height * 3 / 4
        text: "Go To Second Page"
        onClicked: stackView.push("SecondPage.qml")
    }
}

// SecondPage.qml
import QtQuick
import QtQuick.Controls
Page {
    Label {
        anchors.centerIn: parent
        text: "SecondPage.qml"
    }
    Button {
        anchors.horizontalCenter: parent.horizontalCenter
        y: parent.height * 3 / 4
        text: "Back"
        onClicked: stackView.pop()
    }
}

// AppToolBar.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
ToolBar {
    RowLayout {
        width: parent.width
        ToolButton {
            enabled: stackView.depth > 1
            icon.source: "chevron-left-32.svg"
            opacity: enabled ? 1 : 0
            onClicked: stackView.pop()
        }
        Label {
            Layout.fillWidth: true
            text: "Page Title"
            horizontalAlignment: Qt.AlignHCenter
        }
        ToolButton {
            icon.source: "handle-vertical-32.svg"
        }
    }
}

// chevron-left-32.svg: https://raw.githubusercontent.com/Esri/calcite-ui-icons/master/icons/chevron-left-32.svg

// handle-vertical-32.svg: https://raw.githubusercontent.com/Esri/calcite-ui-icons/master/icons/handle-vertical-32.svg

你可以在线试用!

英文:

As hinted by JarMan, the cause of the problem/confusion is your implementation of Loader makes your StackView implementation ambiguous. I have created a version of your application just using the StackView and made that receive "SecondPage.qml":

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
    header: AppToolBar { }
    StackView {
        id: stackView
        anchors.fill: parent
        initialItem: "MainView.qml"
    }
}

// MainView.qml
import QtQuick
import QtQuick.Controls
Page {
    Label {
        anchors.centerIn: parent
        text: "MainView.qml"
    }
    Button {
        anchors.horizontalCenter: parent.horizontalCenter
        y: parent.height * 3 / 4
        text: "Go To Second Page"
        onClicked: stackView.push("SecondPage.qml")
    }
}

// SecondPage.qml
import QtQuick
import QtQuick.Controls
Page {
    Label {
        anchors.centerIn: parent
        text: "SecondPage.qml"
    }
    Button {
        anchors.horizontalCenter: parent.horizontalCenter
        y: parent.height * 3 / 4
        text: "Back"
        onClicked: stackView.pop()
    }
}

// AppToolBar.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
ToolBar {
    RowLayout {
        width: parent.width
        ToolButton {
            enabled: stackView.depth > 1
            icon.source: "chevron-left-32.svg"
            opacity: enabled ? 1 : 0
            onClicked: stackView.pop()
        }
        Label {
            Layout.fillWidth: true
            text: "Page Title"
            horizontalAlignment: Qt.AlignHCenter
        }
        ToolButton {
            icon.source: "handle-vertical-32.svg"
        }
    }
}

// chevron-left-32.svg: https://raw.githubusercontent.com/Esri/calcite-ui-icons/master/icons/chevron-left-32.svg

// handle-vertical-32.svg: https://raw.githubusercontent.com/Esri/calcite-ui-icons/master/icons/handle-vertical-32.svg

You can Try it Online!

huangapple
  • 本文由 发表于 2023年6月16日 01:36:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定