little breakthrought im QML Layouts

This commit is contained in:
2025-08-26 17:45:06 +02:00
parent a39ce321f2
commit b217f2f9ad
16 changed files with 463 additions and 70 deletions

View File

@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2025 Martin Leutelt <martin.leutelt@basyskom.com>
// SPDX-FileCopyrightText: 2025 basysKom GmbH
//
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick
import QtQuick.Controls
Control {
id: container
required property int column
required property var model
property int sortOrder: Qt.AscendingOrder
signal clicked(int sortOrder)
padding: 8
background: Rectangle {
color: tapHandler.pressed ? "gray" : "lightgray"
TapHandler {
id: tapHandler
onTapped: {
if (container.sortOrder === Qt.AscendingOrder) {
container.sortOrder = Qt.DescendingOrder
} else {
container.sortOrder = Qt.AscendingOrder
}
container.clicked(container.sortOrder)
}
}
}
contentItem: Label {
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
text: container.model.display
}
}

View File

@@ -0,0 +1,26 @@
import QtQuick
import QtQuick.Controls
Control {
id: container
required property int row
required property var model
padding: 8
background: Rectangle {
color: tapHandler.pressed ? "gray" : "lightgray"
TapHandler {
id: tapHandler
}
}
contentItem: Label {
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
text: container.model.display
}
}

161
qml/XMain.qml Normal file
View File

@@ -0,0 +1,161 @@
// SPDX-FileCopyrightText: 2025 Martin Leutelt <martin.leutelt@basyskom.com>
// SPDX-FileCopyrightText: 2025 basysKom GmbH
//
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
ApplicationWindow {
width: 640
height: 480
visible: true
title: Application.displayName
header: ToolBar {
ColumnLayout {
RowLayout {
Label {
text: "Rows"
}
SpinBox {
id: rowSettings
from: 1
to: 20
}
ToolSeparator {}
Label {
text: "Columns"
}
SpinBox {
id: columnSettings
from: 1
to: 20
}
ToolSeparator {}
CheckBox {
id: movableColumnsSetting
text: "Movable columns"
}
ToolSeparator {}
CheckBox {
id: resizableColumnsSetting
text: "Resizable columns"
}
}
RowLayout {
Label {
text: "Selection"
}
ComboBox {
id: selectionSetting
textRole: "text"
valueRole: "value"
model: [
{ text: "disabled", value: TableView.SelectionDisabled },
{ text: "by cells", value: TableView.SelectCells },
{ text: "by rows", value: TableView.SelectRows },
{ text: "by columns", value: TableView.SelectColumns }
]
onCurrentIndexChanged: tableView.selectionModel.clear()
}
Label {
text: "Longpress to start selection, modify selection with CTRL/SHIFT of by mouse"
visible: selectionSetting.currentIndex > 0
}
}
}
}
Rectangle {
id: tableBackground
anchors.fill: parent
color: Application.styleHints.colorScheme === Qt.Light ? palette.mid : palette.midlight
HorizontalHeaderView {
id: horizontalHeader
anchors.left: tableView.left
anchors.top: parent.top
syncView: tableView
movableColumns: movableColumnsSetting.checked
resizableColumns: resizableColumnsSetting.checked
clip: true
boundsBehavior: tableView.boundsBehavior
delegate: HorizontalHeaderViewDelegate {
onClicked: (sortOrder) => tableView.model.sort(column, sortOrder)
}
}
VerticalHeaderView {
id: verticalHeader
anchors.top: tableView.top
anchors.left: parent.left
syncView: tableView
clip: true
boundsBehavior: tableView.boundsBehavior
delegate: VerticalHeaderViewDelegate {}
}
TableView {
id: tableView
anchors.left: verticalHeader.right
anchors.top: horizontalHeader.bottom
anchors.right: parent.right
anchors.bottom: parent.bottom
clip: true
columnSpacing: 1
rowSpacing: 1
rowHeightProvider: (row) => 40
boundsBehavior: TableView.StopAtBounds
selectionModel: ItemSelectionModel {}
selectionBehavior: selectionSetting.currentValue
model: SortFilterModel {
sourceModel: TableModel {
columns: columnSettings.value
rows: rowSettings.value
// when adding a new column its width isn't properly applied to the header, so we do that manually
onColumnsInserted: {
if (columns > 1) {
horizontalHeader.setColumnWidth(columns - 1, tableView.implicitColumnWidth(columns - 1))
}
}
}
}
delegate: TableViewDelegate {
implicitWidth: tableView.width / columnSettings.to
}
ScrollBar.horizontal: ScrollBar {}
ScrollBar.vertical: ScrollBar {}
}
SelectionRectangle {
target: tableView
}
}
}

37
qml/dummyview.qml Normal file
View File

@@ -0,0 +1,37 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Window
ApplicationWindow
{
visible: true
width: 600
height: 400
title: "TableView mit myChildModel"
TableView
{
anchors.fill: parent
clip: true
columnSpacing: 1
rowSpacing: 1
model: myChildModel
delegate: Rectangle
{
implicitWidth: 150
implicitHeight: 40
border.color: "#cccccc"
//color: index % 2 === 0 ? "#f9f9f9" : "#e0e0e0"
Text {
anchors.centerIn: parent
text: display
font.pixelSize: 14
}
}
}
}

65
qml/xqtableview.qml Normal file
View File

@@ -0,0 +1,65 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Window
{
width: 640
height: 480
visible: true
title: qsTr("StringListModel")
TableView
{
id: childTableView
anchors.fill: parent
model: myChildModel // z.B. QStandardItemModel mit 9 Spalten
delegate: Rectangle
{
required property string display
//height: 5
//width: childTableView.width
color : "blue"
border.color: "#ccc"
width: childTableView.width;
RowLayout
{
anchors.fill: parent
anchors.margins: 2
TextField
{
text : display
font.pixelSize: 10
Layout.fillWidth: true
background: Rectangle
{
color : "white"
}
onEditingFinished:
{
console.log("Editing finished, new text is :"+ text + " at index :" + index)
model.names = text //The roles here are defined in model class
}
}
}
}
ScrollBar.horizontal: ScrollBar {}
ScrollBar.vertical: ScrollBar {}
// // Optional: Spaltenbreiten setzen
// onModelChanged: {
// for (let i = 0; i < model.columns; ++i)
// table.setColumnWidth(i, 100)
// }
}
}