QTableView
常用的QTableWidget 就是继承的这 QTableView ,也是常用的数据模型。
代码:
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Table(QWidget):
def __init__(self, arg = None):
super(Table, self).__init__(arg)
self.setWindowTitle("QTableView表格视图控件的例子")
self.resize(500,300)
self.model = QStandardItemModel(4,4)
self.model.setHorizontalHeaderLabels(['标题1','标题2','标题3','标题4'])
for row in range(4):
for column in range(4):
item = QStandardItem("row %s, column %s" % (row, column))
self.model.setItem(row, column, item)
self.tableView = QTableView()
self.tableView.setModel(self.model)
# 下面代码让表格100填满窗口
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
dlgLayout = QVBoxLayout()
dlgLayout.addWidget(self.tableView)
self.setLayout(dlgLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
table = Table()
table.show()
sys.exit(app.exec_())
QListView
常用的信号:
代码:
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication, QWidget , QVBoxLayout , QListView, QMessageBox
from PyQt5.QtCore import QStringListModel
import sys
class ListViewDemo(QWidget):
def __init__(self, parent=None):
super(ListViewDemo, self).__init__(parent)
self.setWindowTitle("QListView 例子")
self.resize(300, 270)
layout = QVBoxLayout()
listView = QListView()
slm = QStringListModel();
self.qList = ['Item 1','Item 2','Item 3','Item 4' ]
slm.setStringList(self.qList)
listView.setModel(slm )
listView.clicked.connect(self.clicked)
layout.addWidget( listView )
self.setLayout(layout)
def clicked(self, qModelIndex):
QMessageBox.information(self, "QListView", "你选择了: "+ self.qList[qModelIndex.row()])
if __name__ == "__main__":
app = QApplication(sys.argv)
win = ListViewDemo()
win.show()
sys.exit(app.exec_())
本章目前讲解这一点吧,后面更加精彩,喜欢的朋友,可以关注博客一同进步与学习。