QTableView这个空间可以绑定一个模型数据用来跟新空间的内容
有如下几个:
- QStringListModel 存储一组字符串
- QStandardItemModel 存储任意层次结构数据
- QDirModel 对文件系统进行封装
- QSqlQueryModel 对SQL查询结构集进行封装
- QSqlTableMode 对SQL中的TABLe进行封装
- QSqlRelationalTableModel 对带有Foreign Key 的SQL Table进行封装
- QSortFilterProxyModel 对另一个model执行 sort and /or filter
以下代码是使用QStandardItemMode对QTableView数据进行封装
系统:win10
py:3.6
qt:5.9
ui-->py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'pttUI.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 537)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 611, 501))
self.tabWidget.setStyleSheet("")
self.tabWidget.setObjectName("tabWidget")
self.tab_1 = QtWidgets.QWidget()
self.tab_1.setStyleSheet("background:rgb(155, 122, 200)\n"
"")
self.tab_1.setObjectName("tab_1")
self.tableView = QtWidgets.QTableView(self.tab_1)
self.tableView.setGeometry(QtCore.QRect(10, 10, 521, 301))
self.tableView.setObjectName("tableView")
self.tabWidget.addTab(self.tab_1, "")
self.tab = QtWidgets.QWidget()
self.tab.setStyleSheet("background:orange")
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 23))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
self.menu_2 = QtWidgets.QMenu(self.menubar)
self.menu_2.setObjectName("menu_2")
self.menu_3 = QtWidgets.QMenu(self.menubar)
self.menu_3.setObjectName("menu_3")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.menubar.addAction(self.menu_3.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "对讲模块"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_1), _translate("MainWindow", "设备列表"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "分组码列表"))
self.menu.setTitle(_translate("MainWindow", "设备查看"))
self.menu_2.setTitle(_translate("MainWindow", "分组查看"))
self.menu_3.setTitle(_translate("MainWindow", "主板信息查看"))
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: PttMain
Description :
Author : houyujiang
date: 2018/1/25 11:08
IDE: PyCharm
-------------------------------------------------
Change Activity:
2018/1/25:
-------------------------------------------------
"""
import sys
from PyQt5 import QtWidgets
from pttUI import Ui_MainWindow
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class myform(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self):
super(myform, self).__init__()
self.setupUi(self)
#设置一个4*4的表格数据模型
self.model = QStandardItemModel(4, 4)
#设置横坐标每项的属性名
self.model.setHorizontalHeaderLabels(['id', '分组码', '东经', '北纬'])
#配置数据,注意!!!需要使用QStandardItem格式的文本
self.model.setItem(0, 0, QStandardItem('186'))
self.model.setItem(0, 1, QStandardItem('0000'))
self.model.setItem(0, 2, QStandardItem('108.1721'))
self.model.setItem(0, 3, QStandardItem('20.1231'))
self.model.setItem(1, 0, QStandardItem('189'))
self.model.setItem(1, 1, QStandardItem('1001'))
self.model.setItem(1, 2, QStandardItem('108.1721'))
self.model.setItem(1, 3, QStandardItem('20.1231'))
self.model.setItem(2, 0, QStandardItem('175'))
self.model.setItem(2, 1, QStandardItem('1002'))
self.model.setItem(2, 2, QStandardItem('108.1721'))
self.model.setItem(2, 3, QStandardItem('20.1231'))
self.model.setItem(3, 0, QStandardItem('152'))
self.model.setItem(3, 1, QStandardItem('1003'))
self.model.setItem(3, 2, QStandardItem('108.1721'))
self.model.setItem(3, 3, QStandardItem('20.1231'))
self.tableView.setModel(self.model)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = myform()
ex.show()
sys.exit(app.exec_())