SqlScreen.qml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //TableWidget.qml
  2. import QtQuick
  3. import QtQuick.Controls
  4. import QtQuick.Window
  5. import QtQuick.Dialogs
  6. import QtQuick.LocalStorage
  7. import EasyModel 1.0
  8. import com.example.database 1.0
  9. //自定义QtQuick 2中的TableView
  10. Item {
  11. id: control
  12. implicitHeight: 300
  13. implicitWidth: 500
  14. // Define the database connection properties
  15. property string dbName: "config.db"
  16. property string dbPath: appdir
  17. DatabaseInterface
  18. {
  19. id: db_cfg
  20. }
  21. //行表头-竖向的
  22. property int verHeaderHeight: 30
  23. property int verHeaderWidth: 150
  24. //列表头-横向的
  25. property int horHeaderHeight: 30
  26. //property int horHeaderWidth: 30
  27. //滚动条
  28. property color scrollBarColor: "cyan"
  29. property int scrollBarWidth: 6
  30. //列宽
  31. property variant columnWidthArr: [30,100,100,100,100,100,200,100,100,100,100,100,100]
  32. property variant table_mqtt_Value: ["id","dtype","ip","port","imei","uid","pwd","clientid","qos","keepalive","sub","pub","offline"]
  33. property variant table_ftp_Value: ["id","ip","port","dir","uid","pwd","mode"]
  34. property variant table_location_Value: ["id","lbs_lat","lbs_lng","gps_lat","gps_lng","gps_elevation"]
  35. property variant table_camera_Value: ["id","type","balance_red","balance_green","balance_blue"]
  36. property variant table_json_Value: ["id","datetime","json_data","json_data"]
  37. property int datacount: 1
  38. EasyTableModel{
  39. id: table_model
  40. horHeader: table_mqtt_Value
  41. //initData: []//myClass.getdata_JsonArray()
  42. // initData: [
  43. }
  44. Rectangle {
  45. id: sqlite_compage
  46. radius: 5
  47. opacity: 1
  48. border.width: 1
  49. border.color: "purple"
  50. width: control.verHeaderWidth
  51. anchors.left: parent.left
  52. anchors.bottom: parent.bottom
  53. anchors.top: parent.top
  54. Label {
  55. id: sqlite_nameLab
  56. width: parent.width
  57. height: 40
  58. text: qsTr("数据库")
  59. anchors.left: parent.left
  60. anchors.top: parent.top
  61. font.pixelSize: 16
  62. horizontalAlignment: Text.AlignHCenter
  63. verticalAlignment: Text.AlignVCenter
  64. // anchors.topMargin: 1
  65. //anchors.leftMargin: 10
  66. }
  67. ComboBox {
  68. id: sqlite_combox
  69. height: 40
  70. anchors.left: parent.left
  71. anchors.top: sqlite_nameLab.bottom
  72. anchors.leftMargin: 12
  73. font.pixelSize: 16
  74. model: ["config.db", "data.db"]
  75. onActivated: {
  76. console.log(displayText)
  77. dbName = displayText
  78. if(displayText===model[1])
  79. {
  80. table_combox.displayText=table_combox.model[6]
  81. table_model.horHeader = table_json_Value
  82. columnWidthArr[1] = 200
  83. columnWidthArr[2] = 500
  84. columnWidthArr[3] = 500
  85. }
  86. else
  87. {
  88. table_combox.displayText=table_combox.model[0]
  89. table_model.horHeader = table_mqtt_Value
  90. columnWidthArr[1] = 100
  91. columnWidthArr[2] = 100
  92. columnWidthArr[3] = 100
  93. }
  94. }
  95. Component.onCompleted: {
  96. dbName = displayText
  97. }
  98. }
  99. SwitchDelegate{
  100. id: sqlite_switch
  101. height: 40
  102. text: qsTr("开关")
  103. anchors.left: sqlite_combox.left
  104. anchors.top: sqlite_combox.bottom
  105. onCheckedChanged: {
  106. if(sqlite_switch.checked)
  107. {
  108. //打开数据库
  109. var dbs = db_cfg.openDatabase(dbName)
  110. if(dbs)
  111. {
  112. sqlite_combox.enabled = false
  113. table_combox.enabled = true
  114. if(sqlite_combox.displayText==="data.db")
  115. {
  116. table_combox.displayText=table_combox.model[6];
  117. }
  118. table_model.table_name = table_combox.displayText
  119. table_model.initData=db_cfg.queryData(table_combox.displayText,datacount)
  120. text_row_count.text = db_cfg.getTable_row(table_combox.displayText)
  121. rowindex_text.text = datacount
  122. }
  123. else
  124. {
  125. sqlite_switch.checked = false
  126. }
  127. console.log("打开数据库",dbs)
  128. }
  129. else
  130. {
  131. sqlite_combox.enabled = true
  132. table_combox.enabled = false
  133. db_cfg.closeDatabase()
  134. console.log("关闭数据库")
  135. //关闭数据库
  136. }
  137. }
  138. }
  139. Label {
  140. id: table_label
  141. text: qsTr("数据表")
  142. height: 40
  143. width: parent.width
  144. anchors.left: parent.left
  145. anchors.top: sqlite_switch.bottom
  146. font.pixelSize: 16
  147. horizontalAlignment: Text.AlignHCenter
  148. verticalAlignment: Text.AlignVCenter
  149. }
  150. ComboBox {
  151. id: table_combox
  152. enabled: false
  153. height: 40
  154. anchors.left: parent.left
  155. anchors.top: table_label.bottom
  156. anchors.leftMargin: 12
  157. font.pixelSize: 16
  158. model: ["mqtt", "mqtt_other","ftp","ftp_other","location","camera","data"]
  159. onActivated: {
  160. console.log(displayText)
  161. table_model.table_name = displayText
  162. if(displayText===model[0]||displayText===model[1])
  163. {
  164. table_model.horHeader = table_mqtt_Value
  165. }
  166. else if(displayText===model[2]||displayText===model[3])
  167. {
  168. table_model.horHeader = table_ftp_Value
  169. }
  170. else if(displayText===model[4])
  171. {
  172. table_model.horHeader = table_location_Value
  173. }
  174. else if(displayText===model[6])
  175. {
  176. table_model.horHeader = table_json_Value
  177. }
  178. table_model.initData=db_cfg.queryData(displayText,datacount)
  179. }
  180. }
  181. Label {
  182. id: rowcount_label
  183. text: qsTr("缓存")
  184. height: 40
  185. width: parent.width
  186. anchors.left: parent.left
  187. anchors.top: table_combox.bottom
  188. font.pixelSize: 16
  189. horizontalAlignment: Text.AlignHCenter
  190. verticalAlignment: Text.AlignVCenter
  191. }
  192. TextArea {
  193. id: text_row_count
  194. enabled: false
  195. color: "red"
  196. text: "0"
  197. height: 40
  198. width: parent.width
  199. anchors.left: parent.left
  200. anchors.top: rowcount_label.bottom
  201. font.pixelSize: 16
  202. horizontalAlignment: Text.AlignHCenter
  203. verticalAlignment: Text.AlignVCenter
  204. }
  205. Label {
  206. id: rowindex_label
  207. text: qsTr("当前页")
  208. height: 40
  209. width: parent.width
  210. anchors.left: parent.left
  211. anchors.top: text_row_count.bottom
  212. font.pixelSize: 16
  213. horizontalAlignment: Text.AlignHCenter
  214. verticalAlignment: Text.AlignVCenter
  215. }
  216. TextArea {
  217. id: rowindex_text
  218. enabled: true
  219. color: "red"
  220. text: "0"
  221. height: 40
  222. width: parent.width
  223. anchors.left: parent.left
  224. anchors.top: rowindex_label.bottom
  225. font.pixelSize: 16
  226. horizontalAlignment: Text.AlignHCenter
  227. verticalAlignment: Text.AlignVCenter
  228. }
  229. Button {
  230. id: setindex_btn
  231. text: "翻页"
  232. height: 40
  233. width: 100
  234. anchors.top: rowindex_text.bottom
  235. anchors.horizontalCenter: parent.horizontalCenter
  236. font.pixelSize: 16
  237. onClicked: {
  238. console.log("翻页",rowindex_text.text)
  239. datacount = rowindex_text.text
  240. table_model.initData=db_cfg.queryData(table_combox.displayText,datacount)
  241. }
  242. }
  243. Button {
  244. id: nextindex_btn
  245. text: "下一页"
  246. height: 40
  247. width: 100
  248. anchors.top: setindex_btn.bottom
  249. anchors.horizontalCenter: parent.horizontalCenter
  250. font.pixelSize: 16
  251. onClicked: {
  252. datacount++;
  253. rowindex_text.text = datacount;
  254. console.log("下一页",rowindex_text.text)
  255. table_model.initData=db_cfg.queryData(table_combox.displayText,datacount)
  256. }
  257. }
  258. // ListView {
  259. // }
  260. }
  261. //表格内容(不包含表头)
  262. TableView{
  263. id: table_view
  264. height: control.implicitHeight
  265. anchors{
  266. left: sqlite_compage.right
  267. right: parent.right
  268. top: parent.top
  269. leftMargin: 30
  270. topMargin: control.horHeaderHeight
  271. }
  272. clip: true
  273. boundsBehavior: Flickable.StopAtBounds
  274. columnSpacing: 1
  275. rowSpacing: 1
  276. //视图的高度
  277. //contentHeight: rowHeightProvider(0) * rows + rowHeightProvider(rows-1)
  278. //视图的宽度
  279. //contentWidth:
  280. //content内容区域边距,但是不影响滚动条的位置
  281. //leftMargin:
  282. //topMargin:
  283. //此属性可以包含一个函数,该函数返回模型中每行的行高
  284. rowHeightProvider: function (row) {
  285. return control.verHeaderHeight;
  286. }
  287. //此属性可以保存一个函数,该函数返回模型中每个列的列宽
  288. columnWidthProvider: function (column) {
  289. return control.columnWidthArr[column];
  290. //return Math.max(1, (table_view.width - leftMargin) / table_view.columns)
  291. }
  292. ScrollBar.vertical: ScrollBar {
  293. id: scroll_vertical
  294. anchors.right: parent.right
  295. anchors.rightMargin: 2
  296. //active: table_view.ScrollBar.vertical.active
  297. //policy: ScrollBar.AsNeeded
  298. contentItem: Rectangle{
  299. visible: (scroll_vertical.size<1.0)
  300. implicitWidth: control.scrollBarWidth
  301. color: control.scrollBarColor
  302. }
  303. }
  304. ScrollBar.horizontal: ScrollBar {
  305. id: scroll_horizontal
  306. anchors.bottom: parent.bottom
  307. anchors.bottomMargin: 2
  308. //active: table_view.ScrollBar.vertical.active
  309. //policy: ScrollBar.AsNeeded
  310. contentItem: Rectangle{
  311. visible: (scroll_horizontal.size<1.0)
  312. implicitHeight: control.scrollBarWidth
  313. color: control.scrollBarColor
  314. }
  315. }
  316. //model是在C++中定义的,和QtC++是类似的
  317. model: table_model
  318. delegate: Rectangle{
  319. color: (model.row%2)?"orange":Qt.darker("orange")
  320. TextInput{
  321. anchors.fill: parent
  322. verticalAlignment: Text.AlignVCenter
  323. horizontalAlignment: Text.AlignHCenter
  324. //elide: Text.ElideRight
  325. selectByMouse: true
  326. selectedTextColor: "black"
  327. selectionColor: "white"
  328. //获取单元格对应的值
  329. text: model.value
  330. onEditingFinished: {
  331. model.edit=text;
  332. //将改变写入到数据库中
  333. console.log("edit",model.value)
  334. if(sqlite_switch.checked)
  335. {
  336. if(table_combox.displayText===table_combox.model[0]||table_combox.displayText===table_combox.model[1])
  337. {
  338. db_cfg.updateData(table_combox.displayText,table_mqtt_Value[index],model.value)
  339. }
  340. if(table_combox.displayText===table_combox.model[2]||table_combox.displayText===table_combox.model[3])
  341. {
  342. db_cfg.updateData(table_combox.displayText,table_ftp_Value[index],model.value)
  343. }
  344. if(table_combox.displayText===table_combox.model[4])
  345. {
  346. db_cfg.updateData(table_combox.displayText,table_location_Value[index],model.value)
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. //横项表头
  354. Item{
  355. id: header_horizontal
  356. anchors{
  357. left: sqlite_compage.right
  358. right: parent.right
  359. leftMargin: 30//control.verHeaderWidth
  360. }
  361. height: control.horHeaderHeight
  362. z: 2
  363. //暂存鼠标拖动的位置
  364. property int posXTemp: 0
  365. MouseArea{
  366. anchors.fill: parent
  367. onPressed: header_horizontal.posXTemp=mouseX;
  368. onPositionChanged: {
  369. if(table_view.contentX+(header_horizontal.posXTemp-mouseX)>0){
  370. table_view.contentX+=(header_horizontal.posXTemp-mouseX);
  371. }else{
  372. table_view.contentX=0;
  373. }
  374. header_horizontal.posXTemp=mouseX;
  375. }
  376. }
  377. Row {
  378. id: header_horizontal_row
  379. anchors.fill: parent
  380. leftPadding: -table_view.contentX
  381. clip: true
  382. spacing: 0
  383. Repeater {
  384. model: table_view.columns > 0 ? table_view.columns : 0
  385. Rectangle {
  386. id: header_horizontal_item
  387. width: table_view.columnWidthProvider(index)+table_view.columnSpacing
  388. height: control.horHeaderHeight
  389. color: "purple"
  390. Text {
  391. anchors.centerIn: parent
  392. text: table_model.headerData(index, Qt.Horizontal)
  393. }
  394. Rectangle{
  395. width: 1
  396. height: parent.height
  397. anchors.right: parent.right
  398. color: "black"
  399. opacity: 0.5
  400. }
  401. MouseArea{
  402. width: 3
  403. height: parent.height
  404. anchors.right: parent.right
  405. cursorShape: Qt.SplitHCursor
  406. onPressed: header_horizontal.posXTemp=mouseX;
  407. onPositionChanged: {
  408. if((header_horizontal_item.width-(header_horizontal.posXTemp-mouseX))>10){
  409. header_horizontal_item.width-=(header_horizontal.posXTemp-mouseX);
  410. }else{
  411. header_horizontal_item.width=10;
  412. }
  413. header_horizontal.posXTemp=mouseX;
  414. control.columnWidthArr[index]=(header_horizontal_item.width-table_view.columnSpacing);
  415. //刷新布局,这样宽度才会改变
  416. table_view.forceLayout();
  417. }
  418. }
  419. }
  420. }
  421. }
  422. }
  423. //竖向表头
  424. Column {
  425. id: header_verical
  426. height: control.implicitHeight
  427. anchors{
  428. left: sqlite_compage.right
  429. top: parent.top
  430. //bottom: parent.bottom
  431. topMargin: control.horHeaderHeight
  432. }
  433. topPadding: -table_view.contentY
  434. z: 2
  435. clip: true
  436. spacing: 1
  437. Repeater {
  438. model: table_view.rows > 0 ? table_view.rows : 0
  439. Rectangle {
  440. width: 30
  441. height: table_view.rowHeightProvider(index)
  442. color: "green"
  443. Text {
  444. anchors.centerIn: parent
  445. text: table_model.headerData(index, Qt.Vertical)
  446. }
  447. }
  448. }
  449. }
  450. }