PyQt5 QTableView基本功能使用

 QTableView基本功能的使用:

1.设置单击选中行

2.设置内容禁止标记

3.设置列标题

4.单击

import sys
from PyQt5.QtWidgets import QTableView, QWidget, QApplication, QVBoxLayout, QDesktopWidget, QAbstractItemView, QMessageBox
from PyQt5.QtGui import QStandardItemModel, QStandardItem


class MainWindow(QWidget):
	def __init__(self):
		super(MainWindow, self).__init__()
		self.model = None
		self.init_data()
		self.init_ui()
		self.connect()
	
	def init_data(self):
		max_row = 3
		max_col = 4
		self.model = QStandardItemModel(max_row, max_col)
		for row in range(0, max_row):
			for col in range(0, max_col):
				value = str(row) + '----' + str(col)
				self.model.setItem(row, col, QStandardItem(value))
		
		col_titles = ['列标题1', '列标题2', '列标题3', '列标题4']
		self.model.setHorizontalHeaderLabels(col_titles)

	def init_ui(self):
		self.setWindowTitle('QTableView Test')

		top_layout = QVBoxLayout()
		
		table_view_layout = QVBoxLayout()
		self.table_view = QTableView()
		self.table_view.setEditTriggers(QAbstractItemView.NoEditTriggers)
		self.table_view.setSelectionBehavior(QAbstractItemView.SelectRows)
		self.table_view.setModel(self.model)
		table_view_layout.addWidget(self.table_view)
		top_layout.addLayout(table_view_layout)

		self.setLayout(top_layout)
		
		self.table_view.setCurrentIndex(self.model.index(2, 0))

	def connect(self):
		self.table_view.clicked.connect(self.table_view_clicked)

	def table_view_clicked(self, index):
		table_row = index.row()
		item = self.model.item(table_row, 0)
		vale = item.text()
		QMessageBox.information(self, '提示', '单击{value}'.format(value=vale))


if __name__ == "__main__":
	app = QApplication(sys.argv)
	main_window = MainWindow()
	main_window.show()
	sys.exit(app.exec_())

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Simon|

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值