pyqt界面刷新(表格数据刷新)、界面传值

https://blog.csdn.net/zx520113/article/details/86598658

 

 

#QTableView组件的使用
from PyQt5.QtWidgets import  QAbstractItemView,QAction, QMenuBar,QTableView, QHeaderView, QFormLayout, QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel
import sys,csv
from PyQt5.QtCore import  *
from PyQt5.QtGui import  QStandardItemModel,QStandardItem

class WindowClass(QMainWindow):
    #如果集成QMainWindow 则self.setLayout(self.layout) 替换成
    """
        widget=QWidget()
        widget.setLayout(self.layout)
        self.setCentralWidget(widget)
    """
    #即可, 注意集成QWidget和集成QMainWindow时候区别

    def __init__(self,parent=None):
        super(WindowClass, self).__init__(parent)
        self.layout=QVBoxLayout()
        self.model=QStandardItemModel(1,1)#存储任意结构数据
        self.model.setHorizontalHeaderLabels(['序号','姓名','年龄','地址'])
        for row in range(5):
            for column in range(4):
                i=QStandardItem("  row %s,column %s"%(row,column))
                self.model.setItem(row,column,i)
        self.tableView=QTableView()
        self.tableView.setModel(self.model)
        self.layout.addWidget(self.tableView)

        #继承QMainWidow使用下面三行代码
        widget=QWidget()
        widget.setLayout(self.layout)
        self.setCentralWidget(widget)

        #继承QWidget则使用下面这样代码
        #self.setLayout(self.layout)

        #设置表格充满这个布局QHeaderView
        #self.tableView.horizontalHeader().setStretchLastSection(True)#最后一列决定充满剩下的界面
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)#所有列自动拉伸,充满界面

        #添加menu菜单栏,注意:QMainWindow 才可以有菜单栏,QWidget没有,因此上面只能采用继承QMainWIndow
        tool = self.addToolBar("File") #这里尝试使用QmenuBar,则此时会卡死,无法完成下面appedRow操作(猜测:可能是因为本身不允许menuBar完成这种操作)
        self.action= QAction("添加", self)
        self.action2=QAction("删除",self)
        self.action3=QAction("更新",self)
        tool.addAction(self.action)
        tool.addAction(self.action2)
        tool.addAction(self.action3)
        tool.actionTriggered[QAction].connect(self.processtrigger)

        self.tableView.setSelectionMode(QAbstractItemView.SingleSelection)#设置只能选中一行
        self.tableView.setEditTriggers(QTableView.NoEditTriggers)#不可编辑
        self.tableView.setSelectionBehavior(QAbstractItemView.SelectRows);#设置只有行选中

    def processtrigger(self,action):
        if action.text()=="添加":
            self.model.appendRow([
                QStandardItem('row %s,column %s' % (11, 11)),
                QStandardItem('row %s,column %s' % (11, 11)),
                QStandardItem('row %s,column %s' % (11, 11)),
                QStandardItem('row %s,column %s' % (11, 11)),
            ])
        if action.text()=="删除":

            r= self.tableView.selectionModel().selectedRows()#获取被选中行
            print(r)#被选中行的列表,每个元素是ModelIndex对象
            #indexs = self.tableView.selectionModel().selection().indexes()#返回结果是QModelIndex类对象,里面有row和column方法获取行列索引
            #print(indexs[0].row())
            if r:
                #下面删除时,选中多行中的最后一行,会被删掉;不选中,则默认第一行删掉
                index=self.tableView.currentIndex()
                print(index.row())
                self.model.removeRow(index.row())

        if action.text() == "更新":
            tem=1
            i = QStandardItem(str(tem))
            self.model.setItem(0, 0, i)



def csvToDict(filename):
    with open(filename) as csvfile:
        reader = csv.DictReader(csvfile)
        dict={}
        for row in reader:
            dict[row['指标']]=row
            del dict[row['指标']]['指标']
    return dict
if __name__=="__main__":
    app=QApplication(sys.argv)
    win=WindowClass()
    win.show()
    sys.exit(app.exec_())

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值