2

I am trying to modify Gallery example. I want to add Button under TabView. So, I put TabView and Button into ColumnLayout, here is code:

import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Window 2.0

Window {
    visible: true
    title: "settings"
    width: 600
    height: 400
ColumnLayout{
    anchors.fill: parent
    TabView {
        anchors.right: parent.right
        anchors.left: parent.left
        Tab {
            title: "Controls"
            Controls { }
        }
        Tab {
            title: "Itemviews"
            Controls { }
        }
        Tab {
            title: "Styles"
            Controls {  }
        }
        Tab {
            title: "Layouts"
            Controls {  }
        }
    }

    RowLayout{
        anchors.right: parent.right
        anchors.left: parent.left
        Button{
            text: "ok"
        }
    }
}

}

However, when I resize window okButton stands under tab controls. How should I fix code?

lnk
  • 593
  • 2
  • 11
  • 27
  • You place Button under TabView as I see, so what is your question? I can't test your example just because of udefined `Controls` but may be you should use `Layout.fillHeight` & `Layout.fillWidth` for `TabView`? But dont't forget to set height of RowLayout – folibis Nov 21 '14 at 23:16
  • @folibis `Controls.qml` is in `Qt Quick Controls - Gallery` example. Just open Qt creator -> welcome and type "Gallery". When I resize window `okButton` stands not under `TabView` but above `TabView` – lnk Nov 22 '14 at 07:36

1 Answers1

1

When you have defined a Layout, each element added has access to specific properties related to the layout itself. These properties are useful to position the element inside the space covered from the layout. Confront what is described here.

Hence, you should modify the ColumnLayout like this:

ColumnLayout {
    anchors.fill: parent                   
    TabView {
        id:frame
        enabled: enabledCheck.checked
        tabPosition: controlPage.item ? controlPage.item.tabPosition : Qt.TopEdge
        Layout.fillHeight: true            // fill the available space vertically 
        Layout.fillWidth: true             // fill the available space horizontally
        Layout.row: 0                      // item in the first row of the column
        anchors.margins: Qt.platform.os === "osx" ? 12 : 2
        Tab {
            id: controlPage
            title: "Controls"
            Controls { }
        }
        Tab {
            title: "Itemviews"
            ModelView { }
        }
        Tab {
            title: "Styles"
            Styles { anchors.fill: parent }
        }
        Tab {
            title: "Layouts"
            Layouts { anchors.fill:parent }
        }
    }

    Button {
        text: "ok"
        Layout.row: 1                       // item in the second row of the column
        Layout.alignment: Qt.AlignCenter    // simple center the button in its spatial slot
    }
}

You don't need a RowLayout for the button. It should be placed in the second row of the ColumnLayout you have defined, since it is a simple component. A sub-layout could be useful in case of multiple elements on the same row, e.g. two or more buttons.

Note also that anchoring is just used for the ColumnLayout to "stretch" and fit the window. All the other operations are executed via the layout properties. For general rules take a look at this other article.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
  • When I resize window `okButton` is placed above controls of tab – lnk Nov 22 '14 at 11:07
  • If you take the gallery example as it is, and change the code as I've done, it shouldn't. If you removed the minimal width that's possible: it depends on the setting of the tab component. That's a totally different issue. Indeed `Controls` has `Layout.minimumWidth: implicitWidth`... you can't just resize the `Window` without considering also the layout of the components. – BaCaRoZzo Nov 22 '14 at 12:17
  • I increased minimumWidth and Height. Now it works fine – lnk Nov 22 '14 at 12:53
  • That's it. :) I'll recommend reading the links I've provided. They will clarify any doubts you can have. – BaCaRoZzo Nov 22 '14 at 12:58