add book
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
# Copyright (c) Daniel Gakwaya.
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(12-TableViewCppModel VERSION 0.1 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick QuickControls2)
|
||||
|
||||
qt_standard_project_setup(REQUIRES 6.5)
|
||||
|
||||
qt_add_executable(app12-TableViewCppModel
|
||||
main.cpp tablemodel.h tablemodel.cpp
|
||||
)
|
||||
|
||||
qt_add_qml_module(app12-TableViewCppModel
|
||||
URI 12-TableViewCppModel
|
||||
VERSION 1.0
|
||||
QML_FILES Main.qml
|
||||
)
|
||||
|
||||
set_target_properties(app12-TableViewCppModel PROPERTIES
|
||||
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
|
||||
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
|
||||
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
|
||||
MACOSX_BUNDLE TRUE
|
||||
WIN32_EXECUTABLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(app12-TableViewCppModel
|
||||
PRIVATE Qt6::Quick Qt6::QuickControls2
|
||||
)
|
||||
|
||||
install(TARGETS app12-TableViewCppModel
|
||||
BUNDLE DESTINATION .
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import TableModel // Import the custom model from C++
|
||||
|
||||
ApplicationWindow {
|
||||
width: 680
|
||||
height: 400
|
||||
visible: true
|
||||
|
||||
HorizontalHeaderView {
|
||||
id: horizontalHeader
|
||||
anchors.left: tableViewId.left
|
||||
anchors.top: parent.top
|
||||
syncView: tableViewId
|
||||
clip: true
|
||||
|
||||
}
|
||||
|
||||
VerticalHeaderView {
|
||||
id: verticalHeader
|
||||
anchors.top: tableViewId.top
|
||||
anchors.left: parent.left
|
||||
syncView: tableViewId
|
||||
clip: true
|
||||
|
||||
}
|
||||
|
||||
TableModel{
|
||||
id: tableModelId
|
||||
}
|
||||
|
||||
TableView {
|
||||
id: tableViewId
|
||||
anchors.left: verticalHeader.right
|
||||
anchors.top: horizontalHeader.bottom
|
||||
anchors.right: parent.right
|
||||
anchors.bottom: parent.bottom
|
||||
|
||||
columnSpacing: 1
|
||||
rowSpacing: 1
|
||||
boundsBehavior: Flickable.StopAtBounds
|
||||
|
||||
model: tableModelId
|
||||
|
||||
delegate: Label {
|
||||
text: model.tabledata
|
||||
width: 100
|
||||
padding: 12
|
||||
|
||||
Rectangle {
|
||||
anchors.fill: parent
|
||||
color: "#efefef"
|
||||
z: -1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Button{
|
||||
text: "See the data"
|
||||
anchors.bottom: parent.bottom
|
||||
onClicked: {
|
||||
console.log(tableModelId.get_display_data(tableModelId.index(0,1)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQuickStyle>
|
||||
#include "tablemodel.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
qmlRegisterType<TableModel>("TableModel", 0, 1, "TableModel");
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
QQuickStyle::setStyle("Material");
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
|
||||
&app, []() { QCoreApplication::exit(-1); },
|
||||
Qt::QueuedConnection);
|
||||
engine.loadFromModule("12-TableViewCppModel", "Main");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "tablemodel.h"
|
||||
|
||||
TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent)
|
||||
{
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
table.append({"John","Doe","32","Farmer","Single","Gounduana","Mestkv"});
|
||||
table.append({"Mary","Jane","27","Teacher","Married","Verkso","Tukk"});
|
||||
|
||||
}
|
||||
|
||||
int TableModel::rowCount(const QModelIndex &) const
|
||||
{
|
||||
return table.size();//Number of rows
|
||||
}
|
||||
|
||||
int TableModel::columnCount(const QModelIndex &) const
|
||||
{
|
||||
return table.at(0).size();//Columns
|
||||
}
|
||||
|
||||
QVariant TableModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
switch (role) {
|
||||
case TableDataRole:
|
||||
{
|
||||
return table.at(index.row()).at(index.column());
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role != Qt::DisplayRole) {
|
||||
return QVariant();
|
||||
}
|
||||
if (orientation == Qt::Horizontal && section == 0) {
|
||||
return "Name";
|
||||
} else if (orientation == Qt::Horizontal && section == 1) {
|
||||
return "Name";
|
||||
} else if (orientation == Qt::Horizontal && section == 2) {
|
||||
return "Age";
|
||||
} else if (orientation == Qt::Horizontal && section == 3) {
|
||||
return "Job";
|
||||
} else if (orientation == Qt::Horizontal && section == 4) {
|
||||
return "Status";
|
||||
} else if (orientation == Qt::Horizontal && section == 5) {
|
||||
return "Country";
|
||||
} else if (orientation == Qt::Horizontal && section == 6) {
|
||||
return "City";
|
||||
}
|
||||
|
||||
if(orientation == Qt::Vertical){
|
||||
return QVariant::fromValue(section + 1) ;
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> TableModel::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[TableDataRole] = "tabledata";
|
||||
return roles;
|
||||
}
|
||||
|
||||
QVariant TableModel::get_display_data(const QModelIndex &index){
|
||||
return data(index,TableDataRole );
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) Daniel Gakwaya.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#ifndef TABLEMODEL_H
|
||||
#define TABLEMODEL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractTableModel>
|
||||
|
||||
class TableModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
enum TableRoles{
|
||||
TableDataRole = Qt::UserRole + 1,
|
||||
};
|
||||
public:
|
||||
explicit TableModel(QObject *parent = nullptr);
|
||||
int rowCount(const QModelIndex & = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex & = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
Q_INVOKABLE
|
||||
QVariant get_display_data(const QModelIndex& index);
|
||||
|
||||
private:
|
||||
QVector<QVector<QString>> table;
|
||||
};
|
||||
|
||||
#endif // TABLEMODEL_H
|
||||
Reference in New Issue
Block a user