QStringListModel

链接:https://pan.baidu.com/s/10ByRRZWqh636EBe5owSprg 
提取码:ret1

import sys
from PyQt5.QtWidgets import QApplication,QWidget,QAbstractItemView
from PyQt5.QtCore import pyqtSlot,QStringListModel,Qt,QModelIndex
from PyQt5 import QtCore
from ui_widget import Ui_Widget
 
class QmyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui=Ui_Widget()
        self.ui.setupUi(self)
 
        self.__provinces=["北京","上海","天津","河北","山东","四川","重庆","广东","河南"]
        self.model=QStringListModel(self)
        self.model.setStringList(self.__provinces)
        self.ui.listView.setModel(self.model)
        self.ui.listView.setEditTriggers(QAbstractItemView.DoubleClicked | QAbstractItemView.SelectedClicked)
    @pyqtSlot()
    def on_btnListReset_clicked(self):
        self.model.setStringList(self.__provinces)
 
    @pyqtSlot()
    def on_btnListAppend_clicked(self):
        lastRow=self.model.rowCount()
        self.model.insertRow(lastRow)
        index=self.model.index(lastRow,0)#获取最后一行地模型索引
        self.model.setData(index,"new Item",Qt.DisplayRole)#设置显示文字
        self.ui.listView.setCurrentIndex(index)#设置选中的行
 
    @pyqtSlot()
    def on_btnListInsert_clicked(self):
        index=self.ui.listView.currentIndex()#当前模型索引
        self.model.insertRow(index.row())
        self.model.setData(index,"inserted item",Qt.DisplayRole)
        self.ui.listView.setCurrentIndex(index)
    @pyqtSlot()
    def on_btnListDel_clicked(self):
        index=self.ui.listView.currentIndex()
        self.model.removeRow(index.row())
    @pyqtSlot()
    def on_btnListClear_clicked(self):
        count=self.model.rowCount()
        self.model.removeRows(0,count)
    @pyqtSlot()
    def on_btnTextDisplay_clicked(self):
        strList=self.model.stringList()
        self.ui.plainTextEdit.clear()
        for strLine in strList:
            self.ui.plainTextEdit.appendPlainText(strLine)
    @pyqtSlot()
    def on_btnTextClear_clicked(self):
        self.ui.plainTextEdit.clear()
    def on_listView_clicked(self,index):
        self.ui.LabInfo.setText("当前项index:row=%d,column=%d"%(index.row(),index.column()))
    
if __name__=="__main__":
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)#自适应缩放
    app=QApplication(sys.argv)
    form=QmyWidget()
    form.show()
    sys.exit(app.exec_())

QStringListModel_第1张图片

你可能感兴趣的:(Pyqt5)