Here's an example of implementing a TableView with HorizontalHeaderView in QML, using a C++ model derived from QAbstractTableModel:
First, let's create the C++ model:
// tablemodel.h
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit TableModel(QObject *parent = nullptr);
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;
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
private:
QVector<QVector<QString>> m_data;
};
#endif // TABLEMODEL_H
// tablemodel.cpp
#include "tablemodel.h"
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
// Sample data
m_data = {
{"Item 1", "Value 1", "Description 1"},
{"Item 2", "Value 2", "Description 2"},
{"Item 3", "Value 3", "Description 3"},
{"Item 4", "Value 4", "Description 4"}
};
}
int TableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_data.count();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_data.isEmpty() ? 0 : m_data.first().count();
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
return m_data[index.row()][index.column()];
}
return QVariant();
}
QHash<int, QByteArray> TableModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "display";
return roles;
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0: return "Name";
case 1: return "Value";
case 2: return "Description";
default: return QVariant();
}
}
return QVariant();
}
Now, let's create the main QML file:
// main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
ApplicationWindow {
visible: true
width: 800
height: 600
title: "TableView Example"
ColumnLayout {
anchors.fill: parent
spacing: 0
// Horizontal Header
HorizontalHeaderView {
id: horizontalHeader
syncView: tableView
Layout.fillWidth: true
delegate: Rectangle {
implicitHeight: 40
color: "#f0f0f0"
border.color: "#d0d0d0"
Text {
anchors.centerIn: parent
text: display
font.bold: true
}
}
}
// Table View
TableView {
id: tableView
Layout.fillWidth: true
Layout.fillHeight: true
clip: true
boundsBehavior: Flickable.StopAtBounds
model: tableModel
delegate: Rectangle {
implicitWidth: 200
implicitHeight: 40
border.color: "#d0d0d0"
Text {
anchors.centerIn: parent
text: display
}
}
ScrollBar.vertical: ScrollBar {}
ScrollBar.horizontal: ScrollBar {}
}
}
}
Finally, here's the main.cpp to set everything up:
// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "tablemodel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
// Create and register the model
TableModel *model = new TableModel();
engine.rootContext()->setContextProperty("tableModel", model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
And the corresponding .pro file:
QT += quick
SOURCES += \
main.cpp \
tablemodel.cpp
HEADERS += \
tablemodel.h
RESOURCES += qml.qrc
This implementation provides:
- A C++ model derived from QAbstractTableModel with sample data
- A QML TableView with a HorizontalHeaderView
- Custom styling for both the header and table cells
- Scrollbars for both horizontal and vertical scrolling
- Proper column headers
- Delegate customization for both header and content cells
Key features:
- The TableView and HorizontalHeaderView are synchronized using the syncView property
- The delegate provides custom styling for each cell
- The model provides both data and header information
- The layout ensures the header stays fixed while the table content scrolls
- The table fills available space in the window
To customize this further, you could:
- Add sorting functionality
- Implement cell editing
- Add selection handling
- Customize the appearance further with different colors, fonts, etc.
- Add context menus
- Implement filtering
Would you like me to elaborate on any of these aspects or show how to implement additional features?