diff --git a/src/controls/TableView.qml b/src/controls/TableView.qml index d0417bfa97be5d318de5ab5cc4db954812f2fe69..88a897e108e9ed3fe47cb8bcf816616ab254f355 100644 --- a/src/controls/TableView.qml +++ b/src/controls/TableView.qml @@ -303,7 +303,12 @@ ScrollView { if (typeof column['createObject'] === 'function') object = column.createObject(root) + else if (object.__view) { + console.warn("TableView::insertColumn(): you cannot add a column to multiple views") + return null + } if (index >= 0 && index <= columnCount && object.Accessible.role === Accessible.ColumnHeader) { + object.__view = root columnModel.insert(index, {columnItem: object}) return object } @@ -616,7 +621,7 @@ ScrollView { delegate: Item { z:-index - width: modelData.width + width: columnCount == 1 ? viewport.width + __verticalScrollBar.width : modelData.width visible: modelData.visible height: headerVisible ? headerStyle.height : 0 @@ -655,7 +660,7 @@ ScrollView { // NOTE: the direction is different from the master branch // so this indicates that I am using an invalid assumption on item ordering onPositionChanged: { - if (pressed) { // only do this while dragging + if (pressed && columnCount > 1) { // only do this while dragging for (var h = columnCount-1 ; h >= 0 ; --h) { if (drag.target.x > headerrow.children[h].x) { repeater.targetIndex = h @@ -680,7 +685,7 @@ ScrollView { } drag.maximumX: 1000 drag.minimumX: -1000 - drag.target: draghandle + drag.target: columnCount > 1 ? draghandle : null } Loader { @@ -708,6 +713,7 @@ ScrollView { anchors.rightMargin: -width/2 width: 16 ; height: parent.height anchors.right: parent.right + enabled: columnCount > 1 onPositionChanged: { var newHeaderWidth = modelData.width + (mouseX - offset) modelData.width = Math.max(minimumSize, newHeaderWidth) @@ -728,7 +734,7 @@ ScrollView { modelData.width = minWidth } onPressedChanged: if (pressed) offset=mouseX - cursorShape: Qt.SplitHCursor + cursorShape: enabled ? Qt.SplitHCursor : Qt.ArrowCursor } } } diff --git a/src/controls/TableViewColumn.qml b/src/controls/TableViewColumn.qml index 2c629dfbdf9f7fc078b31a8eadafd71bba3ac054..f1abc55320fda459b70fd08af1964d70c45be6f1 100644 --- a/src/controls/TableViewColumn.qml +++ b/src/controls/TableViewColumn.qml @@ -49,6 +49,10 @@ import QtQuick 2.1 */ QtObject { + + /*! \internal */ + property Item __view: null + /*! The title text of the column. */ property string title @@ -56,8 +60,10 @@ QtObject { property string role /*! The current width of the column - The default value depends on platform. */ - property int width: 160 + The default value depends on platform. If only one + column is defined, the width expands to the viewport. + */ + property int width: (__view && __view.columnCount === 1) ? __view.viewport.width : 160 /*! The visible status of the column. */ property bool visible: true diff --git a/tests/auto/controls/data/tst_tableview.qml b/tests/auto/controls/data/tst_tableview.qml index d092ae9d599c20f2365c1abce60a571d0b28e457..8763900eff22ad510359f3eb6ae1f816c083cd2c 100644 --- a/tests/auto/controls/data/tst_tableview.qml +++ b/tests/auto/controls/data/tst_tableview.qml @@ -240,6 +240,23 @@ TestCase { table.destroy() } + function test_columnWidth() { + var tableView = Qt.createQmlObject('import QtQuick 2.1; import QtQuick.Controls 1.0; TableView { }', testCase, ''); + compare(tableView.columnCount, 0) + var column = newColumn.createObject(testCase, {title: "title 1"}); + verify(column.__view === null) + compare(column.width, 160) + compare(column.title, "title 1") + tableView.addColumn(column) + compare(column.__view, tableView) + compare(column.width, tableView.viewport.width) + var tableView2 = Qt.createQmlObject('import QtQuick 2.1; import QtQuick.Controls 1.0; TableView { }', testCase, ''); + tableView2.addColumn(column) // should not work + compare(column.__view, tableView) //same as before + tableView2.destroy() + tableView.destroy() + } + function test_dynamicColumns() { var component = Qt.createComponent("tableview/table_dynamiccolumns.qml") compare(component.status, Component.Ready)