Skip to content

Commit

Permalink
Add QmlTableView
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengtianzuo committed Oct 19, 2017
1 parent 3680a1c commit c366e5c
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 0 deletions.
Binary file added QmlTableView/Face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions QmlTableView/QmlTableView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*!
*@file QmlTableView.h
*@brief QmlTableView
*@version 1.0
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
*@author zhengtianzuo
*/
#include "QmlTableView.h"
#include <QDebug>

QmlTableViewModel::QmlTableViewModel() : QAbstractTableModel(0)
{
}

int QmlTableViewModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_aryData.size();
}

int QmlTableViewModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 3;
}

QVariant QmlTableViewModel::data(const QModelIndex &index, int role) const
{
return m_aryData[index.row()][role];
}

QHash<int, QByteArray> QmlTableViewModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[OneRole] = "1";
roles[TwoRole] = "2";
roles[ThreeRole] = "3";
return roles;
}

void QmlTableViewModel::Add(QString one, QString two, QString three)
{
beginInsertRows(QModelIndex(), m_aryData.size(), m_aryData.size());
QVector<QString> list;
list.push_back(one);
list.push_back(two);
list.push_back(three);
m_aryData.push_back(list);
endInsertRows();
}

void QmlTableViewModel::Set(int row, int column, QString text)
{
if (row == -1){return;}
if (column == -1){return;}
beginResetModel();
m_aryData[row][column] = text;
endResetModel();
}

void QmlTableViewModel::Del()
{
if (m_aryData.size() <= 0) return;
beginRemoveRows(QModelIndex(), m_aryData.size() - 1, m_aryData.size() - 1);
m_aryData.removeLast();
endRemoveRows();
}

void QmlTableViewModel::Refresh()
{
beginResetModel();
endResetModel();
}
34 changes: 34 additions & 0 deletions QmlTableView/QmlTableView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
*@file QmlTableView.h
*@brief QmlTableView
*@version 1.0
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
*@author zhengtianzuo
*/
#pragma once
#include <QAbstractTableModel>
#include <QVector>

enum Role {
OneRole,
TwoRole,
ThreeRole
};

class QmlTableViewModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit QmlTableViewModel();
int rowCount(const QModelIndex & parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;

Q_INVOKABLE void Add(QString one, QString two, QString three);
Q_INVOKABLE void Set(int row, int column, QString text);
Q_INVOKABLE void Del();
Q_INVOKABLE void Refresh();
private:
QVector<QVector<QString>> m_aryData;
};
17 changes: 17 additions & 0 deletions QmlTableView/QmlTableView.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#-------------------------------------------------
#
# Copyright (C) 2003-2103 CamelSoft Corporation
#
#-------------------------------------------------

QT += qml quick

CONFIG += c++11

SOURCES += main.cpp \
qmlTableView.cpp

HEADERS += \
qmlTableView.h

RESOURCES += qml.qrc
21 changes: 21 additions & 0 deletions QmlTableView/QmlTableView.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*!
*@file QmlTableView.qml
*@brief QmlTableView
*@version 1.0
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
*@author zhengtianzuo
*/
import QtQuick 2.8
import QtQuick.Controls 1.4

TableView {
property alias tableView: tableView

id: tableView
TableViewColumn {title: "1"; role: "1"; width: 120}
TableViewColumn {title: "2"; role: "2"; width: 120}
TableViewColumn {title: "3"; role: "3"; width: 120}
model: theModel
alternatingRowColors: false
backgroundVisible: false
}
24 changes: 24 additions & 0 deletions QmlTableView/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
*@file main.cpp
*@brief 程序主文件
*@version 1.0
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
*@author zhengtianzuo
*/
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "QmlTableView.h"

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QmlTableViewModel model;
model.Add(QStringLiteral("初始化"), QStringLiteral("999"), QStringLiteral("888"));
engine.rootContext()->setContextProperty("theModel", &model);
engine.load(QUrl(QLatin1String("qrc:/main.qml")));

return app.exec();
}
70 changes: 70 additions & 0 deletions QmlTableView/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*!
*@file main.qml
*@brief 主文件
*@version 1.0
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
*@author zhengtianzuo
*/
import QtQuick 2.8
import QtQuick.Controls 2.1

ApplicationWindow {
id: frmWindow
title: qsTr("QmlTableView")
width: 400
height: 300
visible: true
onClosing: {qmlTableView.enabled = false;}

QmlTableView{
id: qmlTableView
height: frmWindow.height - 40
width: parent.width
tableView.itemDelegate:Rectangle {
TextField{
id: textField
height: 25
text: styleData.value
selectByMouse: true
onEditingFinished: {
theModel.Set(styleData.row, styleData.column, textField.text);
}
visible: (styleData.column !== 0)
}
Image{
id: image
height: 25
width: 25
source: "qrc:/Face.png"
visible: (styleData.column === 0)
}
}
tableView.rowDelegate: Rectangle {
height: 25
}
}

Row{
anchors.bottom: parent.bottom
height: 40
width: parent.width
Button{
text: qsTr("Add")
onClicked: {
theModel.Add("111",222,3.33);
}
}
Button{
text: qsTr("Del")
onClicked: {
theModel.Del();
}
}
Button{
text: qsTr("Refresh")
onClicked: {
theModel.Refresh();
}
}
}
}
7 changes: 7 additions & 0 deletions QmlTableView/qml.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>QmlTableView.qml</file>
<file>Face.png</file>
</qresource>
</RCC>
Binary file added QmlTableView/show.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c366e5c

Please sign in to comment.