Ai
1 Star 0 Fork 10

小麦/PySide6-UI-Demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TableView.py 3.08 KB
一键复制 编辑 原始数据 按行查看 历史
终點起點 提交于 2024-06-28 14:21 +08:00 . 新增部分控件
import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
class Demo(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
mainLayout = QVBoxLayout()
toolBar =QToolBar()
btn = QPushButton('Add Row')
btn.clicked.connect(self.OnAddRowClicked)
toolBar.addWidget(btn)
btn = QPushButton('Add Column')
btn.clicked.connect(self.OnAddColumnClicked)
toolBar.addWidget(btn)
btn = QPushButton('Delete')
btn.clicked.connect(self.OnDelClicked)
toolBar.addWidget(btn)
toolBar.addWidget(QLabel('Update:'))
self.currentEdit = QLineEdit()
toolBar.addWidget(self.currentEdit)
btn = QPushButton('Update')
btn.clicked.connect(self.OnUpdateClicked)
toolBar.addWidget(btn)
toolBar.addWidget(QLabel('Keyword:'))
self.keyEdit = QLineEdit()
toolBar.addWidget(self.keyEdit)
btn = QPushButton('Search')
btn.clicked.connect(self.OnSearchClicked)
toolBar.addWidget(btn)
self.tableView = QTableView()
self.tableData = QStandardItemModel()
self.tableView.setModel(self.tableData)
self.tableData.setHorizontalHeaderLabels(['C1','C2','C3'])
mainLayout.addWidget(toolBar)
mainLayout.addWidget(self.tableView)
self.setLayout(mainLayout)
def OnAddRowClicked(self):
self.tableData.appendRow([QStandardItem(''),QStandardItem(''),QStandardItem('')])
self.parent.OnShowMessage(f'Add row,count {self.tableData.rowCount()}')
def OnAddColumnClicked(self):
columnCount=self.tableData.columnCount()
self.tableData.setColumnCount(columnCount+1)
columnName=[]
for i in range(1,self.tableData.columnCount()+1):
columnName.append(f'C{i}')
self.tableData.setHorizontalHeaderLabels(columnName)
self.parent.OnShowMessage(f'Add column,count {self.tableData.columnCount()}')
def OnDelClicked(self):
currentIndex = self.tableView.currentIndex()
currentItem = self.tableData.itemFromIndex(currentIndex)
if currentItem is not None:
self.parent.OnShowMessage(f"Delete item '{currentItem.text()}'")
currentItem.setText(None)
def OnUpdateClicked(self):
currentIndex = self.tableView.currentIndex()
currentItem = self.tableData.itemFromIndex(currentIndex)
old = currentItem.text()
new = self.currentEdit.text()
currentItem.setText(new)
self.parent.OnShowMessage(f"Update item from '{old}' to '{new}'")
def OnSearchClicked(self):
keyword = self.keyEdit.text()
msg = f"Searh keyword '{keyword}' result '"
for item in self.tableData.findItems(keyword,Qt.MatchFlag.MatchContains):
msg += f'{item.text()},'
msg+="'"
self.parent.OnShowMessage(msg)
def runDemo(parent):
wigdet = Demo(parent)
return wigdet
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/hmz-xm/pyside6-ui-demo.git
git@gitee.com:hmz-xm/pyside6-ui-demo.git
hmz-xm
pyside6-ui-demo
PySide6-UI-Demo
master

搜索帮助