diff options
| author | MohammadHossein Qanbari <mohammad.qanbari@qt.io> | 2024-10-15 16:33:58 +0200 |
|---|---|---|
| committer | MohammadHossein Qanbari <mohammad.qanbari@qt.io> | 2024-11-27 09:03:42 +0100 |
| commit | 0edf656a2ea2c3fec34892b5f878855d0b094af7 (patch) | |
| tree | 10e36c01eca41c44c6fd715e9e175cfe179597d4 /src/quickcontrols/doc/snippets/qtquickcontrols-tableviewdelegate-custom.qml | |
| parent | 77a20c00e64010c661b9121e8c114065ae5346e0 (diff) | |
Controls: Add TableViewDelegate
This patch introduces TableViewDelegate to the Controls module.
Key features:
- Ready-made delegate assignable to TableView
- Handles table drawing using predefined styles
- Usable without customization
- Implements all required properties set by TableView
- Provides API for changing background and label
Test Suite:
- Verifies TableViewDelegate properties and functionalities
- Tests include:
- Verification of selected, current, and content text properties
- Validation of selection behavior and item clicking
BLACKLISTing two tests (dragToSelect and pressAndHoldToSelect), which
will be removed from the BLACKLIST in the next patch (to fix cell
selection on the Android platform).
Implementation derived from TreeViewDelegate, adapted for TableView use.
TreeViewDelegate patch: 0ddb0d4b9b0c70c4fd4058ef4660e38fd933523e
[ChangeLog][Controls] New delegate added: TableViewDelegate
Fixes: QTBUG-114636
Change-Id: Ibb8b0a7622016e0c6fd58d696e507e7bb76daced
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quickcontrols/doc/snippets/qtquickcontrols-tableviewdelegate-custom.qml')
| -rw-r--r-- | src/quickcontrols/doc/snippets/qtquickcontrols-tableviewdelegate-custom.qml | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/quickcontrols/doc/snippets/qtquickcontrols-tableviewdelegate-custom.qml b/src/quickcontrols/doc/snippets/qtquickcontrols-tableviewdelegate-custom.qml new file mode 100644 index 0000000000..450c574a75 --- /dev/null +++ b/src/quickcontrols/doc/snippets/qtquickcontrols-tableviewdelegate-custom.qml @@ -0,0 +1,149 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [file] +import QtQuick +import QtQuick.Controls.Basic +import QtQuick.Layouts +import Qt.labs.qmlmodels + +TableView { + width: 540 + height: 40 * rows + + columnWidthProvider: function(column) { + switch (column) { + case 0: return 220 + case 1: return 260 + case 2: return 60 + default: return -1 + } + } + + //! [delegate] + delegate: TableViewDelegate { + id: tableCell + + checked: column === 0 ? checkBox.checked : tableView.itemAtIndex(tableView.index(row, 0)).checked + selected: checked + + //! [background] + background: Item { + Rectangle { + anchors.fill: parent + anchors.margins: tableCell.current ? 3 : 1 + color: tableCell.selected ? "blue" : "white" + } + + Rectangle { + anchors.fill: parent + color: "transparent" + border.color: "darkblue" + border.width: tableCell.current ? 2 : 0 + } + } + //! [background] + + //! [contentItem] + contentItem: Item { + implicitHeight: 40 + visible: !tableCell.editing + + RowLayout { + anchors.fill: parent + + CheckBox { + id: checkBox + implicitWidth: height + Layout.fillHeight: true + checked: false + visible: tableCell.column === 0 + } + + Text { + Layout.leftMargin: 4 + Layout.fillWidth: true + Layout.fillHeight: true + verticalAlignment: Text.AlignVCenter + color: tableCell.selected ? "white" : "black" + text: tableCell.model.display + } + } + } + //! [contentItem] + + //! [editDelegate] + TableView.editDelegate: FocusScope { + width: parent.width + height: parent.height + + TableView.onCommit: { + let qaim = tableCell.tableView.model + if (!qaim) + return + const index = qaim.index(tableCell.row, tableCell.column) + // instead of the edit role, any custom role supported by the model can be checked + // e.g. if (!tableCell.checked || !tableCell.model.customRole) + if (!tableCell.checked || !tableCell.model.edit) + return + // instead of the edit role, any custom role supported by the model can be set + // e.g. tableCell.model.customRole = textField.text + tableCell.model.edit = textField.text + tableCell.model.display = textField.text + } + + Component.onCompleted: textField.selectAll() + + TextField { + id: textField + anchors.fill: parent + text: tableCell.model.edit ?? tableCell.model.display ?? "" + focus: true + } + } + //! [editDelegate] + } + //! [delegate] + + model: TableModel { + TableModelColumn { display: "name" } + TableModelColumn { display: "address" } + TableModelColumn { display: "quantity" } + + rows: [ + { + name: "Kristian Quan", + address: "123 Company Place, Big City", + quantity: 4, + }, + { + name: "Matthew Rand", + address: "The Orchard, Little Village", + quantity: 2, + }, + { + name: "Eirik Asaki", + address: "497 Park Skyway, Future City", + quantity: 29, + }, + { + name: "Jarek Hanssen", + address: "1023 RivieraDrive, Southern Precinct", + quantity: 45, + }, + { + name: "Charlos Hartmann", + address: "The Manor House, Country Estate", + quantity: 1, + }, + { + name: "Bea King", + address: "Floor 201, Sun Tower, Central City", + quantity: 32, + }, + ] + } + + selectionModel: ItemSelectionModel { } +} +//! [file] |
