Cannot hide QtQuick.TableView columns
-
The documentation for
TableView
suggests usingTableView.contentWidthProvider
to return0
to hide columns. However, this is incorrect as the code ignores a width of zero providing some default width, and complains with:QML TableView: columnWidthProvider did not return a valid width for column: 2
Setting
visible: false
in the delegate also does not seem to work, even when a valid width (for example 1px) is specified. What is the proper way to hide columns in QtQuick.TableView? -
I put together a simple example to demonstrate this problem and found that TableView does indeed honor columnWidthProvider returning a value of zero, as documented. That means there is a problem in my code.
I'll report back here once I figure it out.
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include "tablemodel.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; qmlRegisterType<TableModel>("TableModel", 1, 0, "TableModel"); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
tablemodel.h
#ifndef TABLEMODEL_H #define TABLEMODEL_H #include <QAbstractTableModel> class TableModel : public QAbstractTableModel { Q_OBJECT public: explicit TableModel(QObject *parent = nullptr); // Header: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; // Basic functionality: int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; private: }; #endif // TABLEMODEL_H
tablemodel.cpp
#include "tablemodel.h" #include <QtQml> TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent) { } QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { Q_UNUSED(orientation); Q_UNUSED(role); return QString("Column %1").arg(section); } int TableModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return 100; } int TableModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return 10; } QVariant TableModel::data(const QModelIndex &index, int role) const { Q_UNUSED(role); if (!index.isValid()) return QVariant(); return QString("Cell %1, %2").arg(index.row()).arg(index.column()); } //qmlRegisterType<TableModel>("com.vedanamedia.TableModel", 1, 0, "TableModel");
main.qml
import QtQuick 2.13 import QtQuick.Window 2.13 import TableModel 1.0 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") TableView { anchors.fill: parent model: TableModel { } columnWidthProvider: function(col) { if(col % 2 == 0) { return 0; } else { var nShownColumns = 0 for(var i=0; i < model.columnCount(); i++) { if(i % 2 != 0) { nShownColumns++; } } return width / nShownColumns; } } contentWidth: width onWidthChanged: { if(contentWidth > 0) // prevent crash (Qt bug) forceLayout() } delegate: Rectangle { implicitWidth: 100 implicitHeight: 50 Text { anchors.centerIn: parent text: display } Rectangle { // border-left width: 3 height: parent.height color: 'green' } Rectangle { // border-right width: 3 height: parent.height x: parent.width-3 color: 'red' } } } }
-
@patrickkidd said in Cannot hide QtQuick.TableView columns:
I put together a simple example to demonstrate this problem and found that TableView does indeed honor columnWidthProvider returning a value of zero, as documented. That means there is a problem in my code.
Aha! This is fixed in Qt-5.13.0. My dev build was using 5.12.4 which is why I was getting the bug. I was running my test example against 5.13.0, which shows that it I fixed.