以下為一個範例檔,欄位當中可以勾選。
tableview.py檔
import os, sys
os.chdir(os.path.dirname(__file__))
from PyQt5 import QtCore
from PyQt5 import QtWidgets, uic
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QBrush, QColor
Ui_MainWindow, QtBaseClass = uic.loadUiType('tableview.ui')
class MainUi(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.initializeNetTAB()
def initializeNetTAB(self):
tableModel = QStandardItemModel(self)
tableModel.itemChanged.connect(self.itemChanged)
tableModel.setHorizontalHeaderLabels(['Name', 'import', 'ports', 'pwr/gnd'])
for i in range(4):
item = QStandardItem(f"case{i}")
tableModel.setItem(i, 0, item)
for j in range(1, 4):
item = QStandardItem()
item.setCheckable(True)
item.setEditable(False)
tableModel.setItem(i, j, item)
self.tableView.setModel(tableModel)
def itemChanged(self, item):
if item.isCheckable() == False:
return
if item.checkState():
item.setText('Yes')
item.setForeground((QBrush(QColor(0, 0, 255, 255))))
else:
item.setText('No')
item.setForeground((QBrush(QColor(100, 100, 100, 255))))
if __name__ == "__main__":
def run_app():
app = QtWidgets.QApplication(sys.argv)
window = MainUi()
window.show()
app.exec_()
run_app()
tableview.ui檔
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>582</width>
<height>751</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTableView" name="tableView">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>561</width>
<height>691</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>582</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
(圖一) TableView表格 |
沒有留言:
張貼留言