| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- //TableWidget.qml
- import QtQuick
- import QtQuick.Controls
- import QtQuick.Window
- import QtQuick.Dialogs
- import QtQuick.LocalStorage
- import EasyModel 1.0
- import com.example.database 1.0
- //自定义QtQuick 2中的TableView
- Item {
- id: control
- implicitHeight: 300
- implicitWidth: 500
- // Define the database connection properties
- property string dbName: "config.db"
- property string dbPath: appdir
- DatabaseInterface
- {
- id: db_cfg
- }
- //行表头-竖向的
- property int verHeaderHeight: 30
- property int verHeaderWidth: 150
- //列表头-横向的
- property int horHeaderHeight: 30
- //property int horHeaderWidth: 30
- //滚动条
- property color scrollBarColor: "cyan"
- property int scrollBarWidth: 6
- //列宽
- property variant columnWidthArr: [30,100,100,100,100,100,200,100,100,100,100,100,100]
- property variant table_mqtt_Value: ["id","dtype","ip","port","imei","uid","pwd","clientid","qos","keepalive","sub","pub","offline"]
- property variant table_ftp_Value: ["id","ip","port","dir","uid","pwd","mode"]
- property variant table_location_Value: ["id","lbs_lat","lbs_lng","gps_lat","gps_lng","gps_elevation"]
- property variant table_camera_Value: ["id","type","balance_red","balance_green","balance_blue"]
- property variant table_json_Value: ["id","datetime","json_data","json_data"]
- property int datacount: 1
- EasyTableModel{
- id: table_model
- horHeader: table_mqtt_Value
- //initData: []//myClass.getdata_JsonArray()
- // initData: [
- }
- Rectangle {
- id: sqlite_compage
- radius: 5
- opacity: 1
- border.width: 1
- border.color: "purple"
- width: control.verHeaderWidth
- anchors.left: parent.left
- anchors.bottom: parent.bottom
- anchors.top: parent.top
- Label {
- id: sqlite_nameLab
- width: parent.width
- height: 40
- text: qsTr("数据库")
- anchors.left: parent.left
- anchors.top: parent.top
- font.pixelSize: 16
- horizontalAlignment: Text.AlignHCenter
- verticalAlignment: Text.AlignVCenter
- // anchors.topMargin: 1
- //anchors.leftMargin: 10
- }
- ComboBox {
- id: sqlite_combox
- height: 40
- anchors.left: parent.left
- anchors.top: sqlite_nameLab.bottom
- anchors.leftMargin: 12
- font.pixelSize: 16
- model: ["config.db", "data.db"]
- onActivated: {
- console.log(displayText)
- dbName = displayText
- if(displayText===model[1])
- {
- table_combox.displayText=table_combox.model[6]
- table_model.horHeader = table_json_Value
- columnWidthArr[1] = 200
- columnWidthArr[2] = 500
- columnWidthArr[3] = 500
- }
- else
- {
- table_combox.displayText=table_combox.model[0]
- table_model.horHeader = table_mqtt_Value
- columnWidthArr[1] = 100
- columnWidthArr[2] = 100
- columnWidthArr[3] = 100
- }
- }
- Component.onCompleted: {
- dbName = displayText
- }
- }
- SwitchDelegate{
- id: sqlite_switch
- height: 40
- text: qsTr("开关")
- anchors.left: sqlite_combox.left
- anchors.top: sqlite_combox.bottom
- onCheckedChanged: {
- if(sqlite_switch.checked)
- {
- //打开数据库
- var dbs = db_cfg.openDatabase(dbName)
- if(dbs)
- {
- sqlite_combox.enabled = false
- table_combox.enabled = true
- if(sqlite_combox.displayText==="data.db")
- {
- table_combox.displayText=table_combox.model[6];
- }
- table_model.table_name = table_combox.displayText
- table_model.initData=db_cfg.queryData(table_combox.displayText,datacount)
- text_row_count.text = db_cfg.getTable_row(table_combox.displayText)
- rowindex_text.text = datacount
- }
- else
- {
- sqlite_switch.checked = false
- }
- console.log("打开数据库",dbs)
- }
- else
- {
- sqlite_combox.enabled = true
- table_combox.enabled = false
- db_cfg.closeDatabase()
- console.log("关闭数据库")
- //关闭数据库
- }
- }
- }
- Label {
- id: table_label
- text: qsTr("数据表")
- height: 40
- width: parent.width
- anchors.left: parent.left
- anchors.top: sqlite_switch.bottom
- font.pixelSize: 16
- horizontalAlignment: Text.AlignHCenter
- verticalAlignment: Text.AlignVCenter
- }
- ComboBox {
- id: table_combox
- enabled: false
- height: 40
- anchors.left: parent.left
- anchors.top: table_label.bottom
- anchors.leftMargin: 12
- font.pixelSize: 16
- model: ["mqtt", "mqtt_other","ftp","ftp_other","location","camera","data"]
- onActivated: {
- console.log(displayText)
- table_model.table_name = displayText
- if(displayText===model[0]||displayText===model[1])
- {
- table_model.horHeader = table_mqtt_Value
- }
- else if(displayText===model[2]||displayText===model[3])
- {
- table_model.horHeader = table_ftp_Value
- }
- else if(displayText===model[4])
- {
- table_model.horHeader = table_location_Value
- }
- else if(displayText===model[6])
- {
- table_model.horHeader = table_json_Value
- }
- table_model.initData=db_cfg.queryData(displayText,datacount)
- }
- }
- Label {
- id: rowcount_label
- text: qsTr("缓存")
- height: 40
- width: parent.width
- anchors.left: parent.left
- anchors.top: table_combox.bottom
- font.pixelSize: 16
- horizontalAlignment: Text.AlignHCenter
- verticalAlignment: Text.AlignVCenter
- }
- TextArea {
- id: text_row_count
- enabled: false
- color: "red"
- text: "0"
- height: 40
- width: parent.width
- anchors.left: parent.left
- anchors.top: rowcount_label.bottom
- font.pixelSize: 16
- horizontalAlignment: Text.AlignHCenter
- verticalAlignment: Text.AlignVCenter
- }
- Label {
- id: rowindex_label
- text: qsTr("当前页")
- height: 40
- width: parent.width
- anchors.left: parent.left
- anchors.top: text_row_count.bottom
- font.pixelSize: 16
- horizontalAlignment: Text.AlignHCenter
- verticalAlignment: Text.AlignVCenter
- }
- TextArea {
- id: rowindex_text
- enabled: true
- color: "red"
- text: "0"
- height: 40
- width: parent.width
- anchors.left: parent.left
- anchors.top: rowindex_label.bottom
- font.pixelSize: 16
- horizontalAlignment: Text.AlignHCenter
- verticalAlignment: Text.AlignVCenter
- }
- Button {
- id: setindex_btn
- text: "翻页"
- height: 40
- width: 100
- anchors.top: rowindex_text.bottom
- anchors.horizontalCenter: parent.horizontalCenter
- font.pixelSize: 16
- onClicked: {
- console.log("翻页",rowindex_text.text)
- datacount = rowindex_text.text
- table_model.initData=db_cfg.queryData(table_combox.displayText,datacount)
- }
- }
- Button {
- id: nextindex_btn
- text: "下一页"
- height: 40
- width: 100
- anchors.top: setindex_btn.bottom
- anchors.horizontalCenter: parent.horizontalCenter
- font.pixelSize: 16
- onClicked: {
- datacount++;
- rowindex_text.text = datacount;
- console.log("下一页",rowindex_text.text)
- table_model.initData=db_cfg.queryData(table_combox.displayText,datacount)
- }
- }
- // ListView {
- // }
- }
- //表格内容(不包含表头)
- TableView{
- id: table_view
- height: control.implicitHeight
- anchors{
- left: sqlite_compage.right
- right: parent.right
- top: parent.top
- leftMargin: 30
- topMargin: control.horHeaderHeight
- }
- clip: true
- boundsBehavior: Flickable.StopAtBounds
- columnSpacing: 1
- rowSpacing: 1
- //视图的高度
- //contentHeight: rowHeightProvider(0) * rows + rowHeightProvider(rows-1)
- //视图的宽度
- //contentWidth:
- //content内容区域边距,但是不影响滚动条的位置
- //leftMargin:
- //topMargin:
- //此属性可以包含一个函数,该函数返回模型中每行的行高
- rowHeightProvider: function (row) {
- return control.verHeaderHeight;
- }
- //此属性可以保存一个函数,该函数返回模型中每个列的列宽
- columnWidthProvider: function (column) {
- return control.columnWidthArr[column];
- //return Math.max(1, (table_view.width - leftMargin) / table_view.columns)
- }
- ScrollBar.vertical: ScrollBar {
- id: scroll_vertical
- anchors.right: parent.right
- anchors.rightMargin: 2
- //active: table_view.ScrollBar.vertical.active
- //policy: ScrollBar.AsNeeded
- contentItem: Rectangle{
- visible: (scroll_vertical.size<1.0)
- implicitWidth: control.scrollBarWidth
- color: control.scrollBarColor
- }
- }
- ScrollBar.horizontal: ScrollBar {
- id: scroll_horizontal
- anchors.bottom: parent.bottom
- anchors.bottomMargin: 2
- //active: table_view.ScrollBar.vertical.active
- //policy: ScrollBar.AsNeeded
- contentItem: Rectangle{
- visible: (scroll_horizontal.size<1.0)
- implicitHeight: control.scrollBarWidth
- color: control.scrollBarColor
- }
- }
- //model是在C++中定义的,和QtC++是类似的
- model: table_model
- delegate: Rectangle{
- color: (model.row%2)?"orange":Qt.darker("orange")
- TextInput{
- anchors.fill: parent
- verticalAlignment: Text.AlignVCenter
- horizontalAlignment: Text.AlignHCenter
- //elide: Text.ElideRight
- selectByMouse: true
- selectedTextColor: "black"
- selectionColor: "white"
- //获取单元格对应的值
- text: model.value
- onEditingFinished: {
- model.edit=text;
- //将改变写入到数据库中
- console.log("edit",model.value)
- if(sqlite_switch.checked)
- {
- if(table_combox.displayText===table_combox.model[0]||table_combox.displayText===table_combox.model[1])
- {
- db_cfg.updateData(table_combox.displayText,table_mqtt_Value[index],model.value)
- }
- if(table_combox.displayText===table_combox.model[2]||table_combox.displayText===table_combox.model[3])
- {
- db_cfg.updateData(table_combox.displayText,table_ftp_Value[index],model.value)
- }
- if(table_combox.displayText===table_combox.model[4])
- {
- db_cfg.updateData(table_combox.displayText,table_location_Value[index],model.value)
- }
- }
- }
- }
- }
- }
- //横项表头
- Item{
- id: header_horizontal
- anchors{
- left: sqlite_compage.right
- right: parent.right
- leftMargin: 30//control.verHeaderWidth
- }
- height: control.horHeaderHeight
- z: 2
- //暂存鼠标拖动的位置
- property int posXTemp: 0
- MouseArea{
- anchors.fill: parent
- onPressed: header_horizontal.posXTemp=mouseX;
- onPositionChanged: {
- if(table_view.contentX+(header_horizontal.posXTemp-mouseX)>0){
- table_view.contentX+=(header_horizontal.posXTemp-mouseX);
- }else{
- table_view.contentX=0;
- }
- header_horizontal.posXTemp=mouseX;
- }
- }
- Row {
- id: header_horizontal_row
- anchors.fill: parent
- leftPadding: -table_view.contentX
- clip: true
- spacing: 0
- Repeater {
- model: table_view.columns > 0 ? table_view.columns : 0
- Rectangle {
- id: header_horizontal_item
- width: table_view.columnWidthProvider(index)+table_view.columnSpacing
- height: control.horHeaderHeight
- color: "purple"
- Text {
- anchors.centerIn: parent
- text: table_model.headerData(index, Qt.Horizontal)
- }
- Rectangle{
- width: 1
- height: parent.height
- anchors.right: parent.right
- color: "black"
- opacity: 0.5
- }
- MouseArea{
- width: 3
- height: parent.height
- anchors.right: parent.right
- cursorShape: Qt.SplitHCursor
- onPressed: header_horizontal.posXTemp=mouseX;
- onPositionChanged: {
- if((header_horizontal_item.width-(header_horizontal.posXTemp-mouseX))>10){
- header_horizontal_item.width-=(header_horizontal.posXTemp-mouseX);
- }else{
- header_horizontal_item.width=10;
- }
- header_horizontal.posXTemp=mouseX;
- control.columnWidthArr[index]=(header_horizontal_item.width-table_view.columnSpacing);
- //刷新布局,这样宽度才会改变
- table_view.forceLayout();
- }
- }
- }
- }
- }
- }
- //竖向表头
- Column {
- id: header_verical
- height: control.implicitHeight
- anchors{
- left: sqlite_compage.right
- top: parent.top
- //bottom: parent.bottom
- topMargin: control.horHeaderHeight
- }
- topPadding: -table_view.contentY
- z: 2
- clip: true
- spacing: 1
- Repeater {
- model: table_view.rows > 0 ? table_view.rows : 0
- Rectangle {
- width: 30
- height: table_view.rowHeightProvider(index)
- color: "green"
- Text {
- anchors.centerIn: parent
- text: table_model.headerData(index, Qt.Vertical)
- }
- }
- }
- }
- }
|