QStringListModel class

1. Main class:
QStringListModel

(1) Function: handle the data model of the string list, which can be used as the data model of QListView to display and edit the string list on the interface.

2. Main functions:
setStringList()
(1) Function: Initialize the content of the string list of the data model.
(2) Parameters: None.
stringList()
(1) Function: Get the string list in the data model.
(2) Parameters: None.
insertRow(row)
(1) Function: Insert a row before the row row. To insert a row at the end of the list, set the parameter row to the current row number of the list.
(2) Parameters: row represents the row number
index(row, column, parentItem)
(1) Function: Generate a model index according to the passed row number, column number, or model index of the parent item
(2) Parameters: row number, column number and parent item
currentIndex()
(1) Function: Returns the model index of the current item
(2) Parameters: None.
removeRow(index)
(1) function: delete the current item
(2) parameter: index indicates the current model index
removeRows(row,count)
(1) function: delete count rows starting from the row number row
(2) parameter: row indicates the row number ;count indicates the number of deleted rows
setData(index,content,userRole)
(1) Function: set the text title for the item
(2) Parameters: index indicates the model index; content indicates the content of the text title; userRole indicates the role of the enumerated data
setEditTriggers()
(1) Function: Set whether the item of QListView can be edited, and how to enter the editing state.
(2) Parameters: QAbstractItemView.EditTrigger enumeration type combination
such as QAbstractItemView.DoubleClicked: double-click the list item to enter the edit state; QAbstractItemView.SelectedClicked: click the list item to enter the edit state; QAbstractItemView.NoEditTriggers: not editable

3. Model/View structure object and component initialization:
The following Demo is created based on the widgetApp project template, mainly the code of the QmyWidget constructor of the form business logic class, where ui_Widget.py is used to compile the display interface of the project, which is not included in this article Put the code.
The use of QListView and QStringListModel

import sys
from PyQt5.QtWidgets import  QApplication, QWidget,QAbstractItemView
from PyQt5.QtCore import  pyqtSlot, QStringListModel, Qt, QModelIndex
from ui_Widget import Ui_Widget

class QmyWidget(QWidget): 
   def __init__(self, parent=None):
      super().__init__(parent)   #调用父类构造函数,创建窗体
      self.ui=Ui_Widget()        #创建UI对象
      self.ui.setupUi(self)      #构造UI界面
	
	##先定义一个省份的字符串列表
      self.__provinces=["北京","上海","天津","河北",
                       "山东","四川","重庆","广东","河南"]
   
   ##定义一个数据类型为QStringListModel
      self.model=QStringListModel(self)
   
   ##将字符串列表的内容初始化为数据模型的数据内容
      self.model.setStringList(self.__provinces)
   
   ##视图组件设置关联一个数据类型
      self.ui.listView.setModel(self.model)
      self.ui.listView.setEditTriggers(QAbstractItemView.DoubleClicked | 
      QAbstractItemView.SelectedClicked)

After such initialization, the component listView on the interface will display the contents of the initialized string list.

4. Control operation:
(1) Add item: add a line at the end of the list;
(2) Insert item: insert a line before the current line in the list

##  =================自定义功能函数=================================
        
##  ==========由connectSlotsByName() 自动连接的槽函数===============        
   @pyqtSlot()    ##重设模型数据内容
   def on_btnList_Reset_clicked(self):   
      self.model.setStringList(self.__provinces); 

   @pyqtSlot()  ##添加项
   def on_btnList_Append_clicked(self):   
      lastRow=self.model.rowCount()
      self.model.insertRow(lastRow)   #在尾部插入一空行
      index=self.model.index(lastRow,0)  #获取最后一行的ModelIndex
      self.model.setData(index,"new item",Qt.DisplayRole) #设置显示文字
      self.ui.listView.setCurrentIndex(index)  #设置当前选中的行

   @pyqtSlot()    ##插入项
   def on_btnList_Insert_clicked(self):   
      index=self.ui.listView.currentIndex()   #当前 modelIndex
      self.model.insertRow(index.row())
      self.model.setData(index,"inserted item", Qt.DisplayRole)  #设置显示文字
      ##        self.model.setData(index,Qt.AlignRight, Qt.TextAlignmentRole) #设置对齐方式,不起作用
      self.ui.listView.setCurrentIndex(index) #设置当前选中的行


   @pyqtSlot()  ##删除当前项
   def on_btnList_Delete_clicked(self):   
      index=self.ui.listView.currentIndex() #获取当前 modelIndex
      self.model.removeRow(index.row()); #删除当前行
        
   @pyqtSlot()    ##清空列表
   def on_btnList_Clear_clicked(self): 
      count=self.model.rowCount()
      self.model.removeRows(0,count)

   @pyqtSlot()    ##清空文本
   def on_btnText_Clear_clicked(self):
      self.ui.plainTextEdit.clear()

   @pyqtSlot()    ##显示数据模型的内容
   def on_btnText_Display_clicked(self):  
      strList=self.model.stringList()  #列表类型
      self.ui.plainTextEdit.clear()
      for strLine in strList:
         self.ui.plainTextEdit.appendPlainText(strLine)

   def on_listView_clicked(self,index):
      self.ui.LabInfo.setText("当前项index: row=%d, column=%d"
                             %(index.row(),index.column()))

Guess you like

Origin blog.csdn.net/qq_35412059/article/details/126068079