forked from zhengtianzuo/QtQuickExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3680a1c
commit c366e5c
Showing
9 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.