久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      1. <i id='JyYrf'><tr id='JyYrf'><dt id='JyYrf'><q id='JyYrf'><span id='JyYrf'><b id='JyYrf'><form id='JyYrf'><ins id='JyYrf'></ins><ul id='JyYrf'></ul><sub id='JyYrf'></sub></form><legend id='JyYrf'></legend><bdo id='JyYrf'><pre id='JyYrf'><center id='JyYrf'></center></pre></bdo></b><th id='JyYrf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='JyYrf'><tfoot id='JyYrf'></tfoot><dl id='JyYrf'><fieldset id='JyYrf'></fieldset></dl></div>

        <small id='JyYrf'></small><noframes id='JyYrf'>

      2. <legend id='JyYrf'><style id='JyYrf'><dir id='JyYrf'><q id='JyYrf'></q></dir></style></legend>

        • <bdo id='JyYrf'></bdo><ul id='JyYrf'></ul>

      3. <tfoot id='JyYrf'></tfoot>

        如何在 python 和 qml 中自動插入/編輯 QAbstractList

        how to insert/edit QAbstractListModel in python and qml updates automatically?(如何在 python 和 qml 中自動插入/編輯 QAbstractListModel 更新?)

          <tbody id='x1HGt'></tbody>
          <bdo id='x1HGt'></bdo><ul id='x1HGt'></ul>
        • <tfoot id='x1HGt'></tfoot>

            • <i id='x1HGt'><tr id='x1HGt'><dt id='x1HGt'><q id='x1HGt'><span id='x1HGt'><b id='x1HGt'><form id='x1HGt'><ins id='x1HGt'></ins><ul id='x1HGt'></ul><sub id='x1HGt'></sub></form><legend id='x1HGt'></legend><bdo id='x1HGt'><pre id='x1HGt'><center id='x1HGt'></center></pre></bdo></b><th id='x1HGt'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='x1HGt'><tfoot id='x1HGt'></tfoot><dl id='x1HGt'><fieldset id='x1HGt'></fieldset></dl></div>

            • <legend id='x1HGt'><style id='x1HGt'><dir id='x1HGt'><q id='x1HGt'></q></dir></style></legend>

                  <small id='x1HGt'></small><noframes id='x1HGt'>

                  本文介紹了如何在 python 和 qml 中自動插入/編輯 QAbstractListModel 更新?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試插入/編輯從 pyqt5 中的 QAbstractListModel 子類化的 python 列表.這個 python 列表是在 qml 中 ListView 元素的 model 屬性中讀取的.我在 qml 中顯示數據沒有問題.當我嘗試將新數據附加到 python 列表中時出現問題.

                  i am trying to insert/edit a python list that is subclassed from QAbstractListModel in pyqt5. this python list is read in the model property of ListView element in qml. i have no issues displaying the data in qml. problem arises when i try to append new data into the python list.

                  以下是我目前所做的:

                  main.py:

                  import sys, model2
                  from PyQt5.QtCore import QUrl
                  from PyQt5.QtWidgets import QApplication
                  from PyQt5.QtQuick import QQuickView
                  
                  class MainWindow(QQuickView):
                      def __init__(self, parent=None):
                          super().__init__(parent)
                  
                          self.model = model2.PersonModel()
                          self.rootContext().setContextProperty('PersonModel', self.model)
                          self.rootContext().setContextProperty('MainWindow', self)
                          self.setSource(QUrl('test2.qml'))
                  
                  myApp = QApplication(sys.argv)
                  ui = MainWindow()
                  ui.show()
                  sys.exit(myApp.exec_())
                  

                  model2.py

                  from PyQt5.QtCore import QAbstractListModel, Qt, pyqtSignal, pyqtSlot
                  
                  class PersonModel(QAbstractListModel):
                  
                      Name = Qt.UserRole + 1
                      Age = Qt.UserRole + 2
                  
                      personChanged = pyqtSignal()
                  
                      def __init__(self, parent=None):
                          super().__init__(parent)
                          self.persons = [
                              {'name': 'jon', 'age': 20},
                              {'name': 'jane', 'age': 25}
                          ]
                  
                      def data(self, QModelIndex, role):
                          row = QModelIndex.row()
                          if role == self.Name:
                              return self.persons[row]["name"]
                          if role == self.Age:
                              return self.persons[row]["age"]
                  
                      def rowCount(self, parent=None):
                          return len(self.persons)
                  
                      def roleNames(self):
                          return {
                              Qt.UserRole + 1: b'name',
                              Qt.UserRole + 2: b'age'
                          }
                  
                      @pyqtSlot()
                      def addData(self):
                          self.beginResetModel()
                          self.persons = self.persons.append({'name': 'peter', 'age': 22})
                          self.endResetModel()
                          print(self.persons)
                  
                      @pyqtSlot()
                      def editData(self):
                          print(self.model.persons)
                  

                  test2.qml:

                  import QtQuick 2.6
                  import QtQuick.Controls 2.2
                  
                  Rectangle {
                      anchors.fill: parent
                      color: "lightgrey"
                  
                      ListView {
                          id: listExample
                          anchors.fill: parent
                          model: PersonModel
                          delegate: Text {
                              text: name + " " + age
                          }
                      }
                  
                      Button {
                          width: 50
                          height: 25
                          anchors.bottom: parent.bottom
                          text: "add"
                          onClicked: {
                              console.log("qml adding")
                              PersonModel.addData()
                          }
                      }
                  
                      .
                      .
                      .
                  }
                  

                  當我單擊調用 model2.py 中的 addData 方法的添加按鈕時發生錯誤.錯誤在于 rowCount 并且錯誤消息說 TypeError: object of type 'NoneType' has no len().我是否必須發出更改或傳遞一些索引和角色值,以便 qml 知道什么是新/舊并僅相應地反映更改?

                  error occurs when i click the add button which calls the addData method in model2.py. error lies in the rowCount and error message says TypeError: object of type 'NoneType' has no len(). do i have to emit the changes or pass in some index and role value so qml knows what is new/old and only reflect the changes accordingly?

                  非常感謝任何形式的指導!

                  any form of guidance is greatly appreciated!

                  推薦答案

                  你得到的錯誤是由下面這行代碼引起的:

                  The error you get is caused by the following line of code:

                  self.persons = self.persons.append({'name': 'peter', 'age': 22})
                  

                  這是因為 append 函數沒有返回任何東西,所以它是為了將 None 分配給 self.persons

                  It is caused because the append function does not return anything, so it was meant to assign None to self.persons

                  要插入新數據,您必須調用 beginInsertRows()endInsertRows() 來通知視圖更改.

                  To insert new data you must call beginInsertRows() and endInsertRows() to notify the view of the change.

                  數據方法必須與文檔中顯示的相同,即必須具有以下格式:

                  the data method must be identical to that shown in the documentation, ie it must have the following format:

                  def data(self, index, role=Qt.DisplayRole):
                  

                  與rowCount方法相同:

                  The same with the rowCount method:

                  def rowCount(self, parent=QModelIndex()):
                  

                  我已經實現了 addPerson、editPerson 和 deletePerson 方法,它們分別添加、編輯和刪除列表中的數據.我還在 .qml 中添加了必要的項目以便能夠對其進行測試.

                  I have implemented the methods addPerson, editPerson and deletePerson that adds, edits and deletes a data from the list respectively. Also I added the necessary items to the .qml to be able to test it.

                  model2.py

                  from PyQt5.QtCore import QAbstractListModel, Qt, pyqtSignal, pyqtSlot, QModelIndex    
                  
                  class PersonModel(QAbstractListModel):
                  
                      NameRole = Qt.UserRole + 1
                      AgeRole = Qt.UserRole + 2
                  
                      personChanged = pyqtSignal()
                  
                      def __init__(self, parent=None):
                          super().__init__(parent)
                          self.persons = [
                              {'name': 'jon', 'age': 20},
                              {'name': 'jane', 'age': 25}
                          ]
                  
                      def data(self, index, role=Qt.DisplayRole):
                          row = index.row()
                          if role == PersonModel.NameRole:
                              return self.persons[row]["name"]
                          if role == PersonModel.AgeRole:
                              return self.persons[row]["age"]
                  
                      def rowCount(self, parent=QModelIndex()):
                          return len(self.persons)
                  
                      def roleNames(self):
                          return {
                              PersonModel.NameRole: b'name',
                              PersonModel.AgeRole: b'age'
                          }
                  
                      @pyqtSlot(str, int)
                      def addPerson(self, name, age):
                          self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
                          self.persons.append({'name': name, 'age': age})
                          self.endInsertRows()
                  
                      @pyqtSlot(int, str, int)
                      def editPerson(self, row, name, age):
                          ix = self.index(row, 0)
                          self.persons[row] = {'name': name, 'age': age}
                          self.dataChanged.emit(ix, ix, self.roleNames())
                  
                      @pyqtSlot(int)
                      def deletePerson(self, row):
                          self.beginRemoveColumns(QModelIndex(), row, row)
                          del self.persons[row]
                          self.endRemoveRows()
                  

                  test2.qml

                  import QtQuick 2.6
                  import QtQuick.Controls 2.2
                  
                  Rectangle {
                      anchors.fill: parent
                      color: "lightgrey"
                  
                      ListView {
                          id: listExample
                          anchors.fill: parent
                          model: PersonModel
                          delegate:
                              Item {
                              width: 200
                              height: 60
                              Row {
                                  Text {
                                      width: 60
                                      text:  name + " " + age
                                      horizontalAlignment: Text.AlignHCenter
                                      anchors.verticalCenter: parent.verticalCenter
                                  }
                                  Button{
                                      width: 20
                                      text: "+"
                                      onClicked: PersonModel.editPerson(index, name, age+1)
                                  }
                                  Button{
                                      width: 20
                                      text: "-"
                                      onClicked: PersonModel.editPerson(index, name, age-1)
                                  }
                                  Button{
                                      width: 20
                                      text: "X"
                                      onClicked: PersonModel.deletePerson(index)
                                  }
                              }
                          }
                      }
                  
                      Button {
                          width: 50
                          height: 25
                          anchors.bottom: parent.bottom
                          anchors.right: parent.right
                          text: "add"
                          onClicked: {
                              console.log("qml adding")
                              PersonModel.addPerson("luis", 22)
                          }
                      }
                  }
                  

                  .py

                  @pyqtSlot(int, str, int)
                  def insertPerson(self, row, name, age):
                      self.beginInsertRows(QModelIndex(), row, row)
                      self.persons.insert(row, {'name': name, 'age': age})
                      self.endInsertRows()
                  

                  .qml

                   PersonModel.insertPerson(2, "luis", 1111)
                  

                  這篇關于如何在 python 和 qml 中自動插入/編輯 QAbstractListModel 更新?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                    <bdo id='AMAm5'></bdo><ul id='AMAm5'></ul>

                      <legend id='AMAm5'><style id='AMAm5'><dir id='AMAm5'><q id='AMAm5'></q></dir></style></legend>
                      <i id='AMAm5'><tr id='AMAm5'><dt id='AMAm5'><q id='AMAm5'><span id='AMAm5'><b id='AMAm5'><form id='AMAm5'><ins id='AMAm5'></ins><ul id='AMAm5'></ul><sub id='AMAm5'></sub></form><legend id='AMAm5'></legend><bdo id='AMAm5'><pre id='AMAm5'><center id='AMAm5'></center></pre></bdo></b><th id='AMAm5'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='AMAm5'><tfoot id='AMAm5'></tfoot><dl id='AMAm5'><fieldset id='AMAm5'></fieldset></dl></div>

                        <tfoot id='AMAm5'></tfoot>

                          <tbody id='AMAm5'></tbody>

                            <small id='AMAm5'></small><noframes id='AMAm5'>

                            主站蜘蛛池模板: 精品成人佐山爱一区二区 | 九九精品在线 | 一级毛片免费完整视频 | 亚洲成人精 | 国内精品久久精品 | 日韩一区精品 | 精品久久国产 | 97在线超碰 | 天天看天天爽 | 99久久精品国产一区二区三区 | 国产欧美日韩 | 久久久久久久av | 亚洲福利免费 | 97热在线 | 国产wwwcom | 成人av网页| 狠狠操狠狠操 | 成人福利视频 | 99国产视频 | 中文字幕日韩欧美一区二区三区 | 日韩精品一二三 | 伊人狠狠干 | 国产精品资源在线 | 剑来高清在线观看 | 国产精品福利久久久 | 亚洲国产精品久久久久秋霞不卡 | 日韩中文一区二区三区 | 男女羞羞视频在线免费观看 | 日韩不卡三区 | 免费一级毛片 | 国产免费拔擦拔擦8x高清 | 久久久这里都是精品 | 中文字幕免费在线观看 | 久久免费精品视频 | www.久久| 精品一区二区三区免费毛片 | 国产一区二区欧美 | 99免费精品视频 | 亚洲人成在线播放 | 亚洲二区视频 | 99精品国产一区二区青青牛奶 |