我在网上搜索,但找不到如何用python在qml文件中选择listview中的行。我测试,但每次都有错误。 我刚开始qml,也许基础不好。所以我的问题是,用我的代码,是否可以访问我的ListView?如果是,如何在启动应用程序中选择第二项(例如)?你知道吗
当我这么做的时候:
print(win.findChild(QObject, "listview22").setCurrentIndex(2)))
我有这个消息
AttributeError: 'QQuickItem' object has no attribute 'setCurrentIndex'
但是listview22是一个listview而不是一个项。你知道吗
谢谢你的帮助。你知道吗
附言:我刚开始qml,所以如果我的脚本不好告诉我,然后我可以学习良好的编程
你知道吗主.qml你知道吗
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
ApplicationWindow {
title: qsTr("WoodMan Training")
width: 1000
height: 700
visible: true
GridLayout {
id: grid
anchors.fill: parent
columns: 3
anchors.margins: 0
columnSpacing: 0
ColumnLayout {
Layout.columnSpan: 1
Layout.row: 2
Layout.column: 0
Layout.fillWidth: true
Layout.fillHeight: true
Layout.margins: 10
Layout.maximumWidth: 250
// ---
RowLayout{
Main_ListView{objectName: "lstGroupe"; id:lstGroupe; pyModel: ModelGroupe; pyClass: ModelGroupe}
}
Item {Layout.fillWidth: true; Layout.fillHeight: true}
}
}
}
主要_列表视图.qml你知道吗
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
Rectangle {
property var pyClass: 0
property var pyModel: 0
border.color: "red"
Layout.fillWidth: true
height:150
ListView {
anchors.topMargin: 10
anchors.bottomMargin: 10
anchors.leftMargin: 10
anchors.fill: parent
id: listview22
objectName: "listview22"
model: pyModel
clip: true // --- Empeche la surbrillance de sortir du cadre
delegate: Component {
Item {
width: 200
height: 20
property int item_id: iid
Row {
Text {
anchors.verticalCenter: parent.verticalCenter
width: 60
text: " " + libelle
}
}
MouseArea {
anchors.fill: parent
onClicked: {
listview22.currentIndex = index
pyClass.onClickItem(item_id)
}
}
}
}
highlight: Rectangle {color: '#CDCDCD'}
focus: true
}
Button {
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.rightMargin: 5
anchors.bottomMargin: 5
width: 28
height: 25
iconSource: "ico/math-add-icon.png"
onClicked: {
//console.log("qml adding")
pyClass.onClickInsert(22, "aluis")
}
}
}
你知道吗主.py你知道吗
from PyQt5.QtCore import QAbstractListModel, Qt, pyqtSignal, pyqtSlot, QModelIndex
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtGui import *#QGuiApplication, QColor, QStandardItemModel, QStandardItem
from PyQt5.QtQml import *#QQmlApplicationEngine, QQmlComponent
from PyQt5.QtCore import *#QUrl, Qt, QCoreApplication, QAbstractListModel, QModelIndex, QTimer, qsrand, qrand, QTime, QObject
from PyQt5.QtQuick import *#QQuickItem
class gGroupe(QAbstractListModel):
iid = Qt.UserRole + 1
libelle = Qt.UserRole + 2
data_change = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self.liste = []
self.insert_list()
self.liste2 = {}
self.createIndex(0,0,125)
self.sort(1, order = Qt.DescendingOrder)
# ------------------------------------------------------------------
def roleNames(self):
return {
gGroupe.iid: b'iid',
gGroupe.libelle: b'libelle',
}
def data(self, index, role=Qt.DisplayRole):
row = index.row()
if role == gGroupe.iid:
return self.liste[row]["iid"]
if role == gGroupe.libelle:
return self.liste[row]["libelle"]
def rowCount(self, parent=QModelIndex()):
return len(self.liste)
# ------------------------------------------------------------------
def insert_list(self):
self.liste = [
{'iid': 10, 'libelle': 'groupe10'},
{'iid': 11, 'libelle': 'groupe11'},
{'iid': 12, 'libelle': 'groupe12'},
]
def delete(self, row):
self.beginRemoveRows(QModelIndex(), row, row)
del self.liste[row]
self.endRemoveRows()
def delete_all(self):
self.beginRemoveRows(QModelIndex(), 0, len(self.liste))
del self.liste[row]
self.endRemoveRows()
def select(self):
pass
@pyqtSlot(int, str)
def onClickInsert(self, iid=10, libelle='toto'):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
self.liste.append({'iid': iid, 'libelle': libelle})
self.endInsertRows()
return self.index(len(self.liste)-1)
@pyqtSlot(int)
def onClickItem(self, iid):
print(iid)
"""
@pyqtSlot(int, str, int)
def editPerson(self, row, name, age):
ix = self.index(row, 0)
self.persons[row] = {'iid': 22, 'name': name, 'age': age}
self.dataChanged.emit(ix, ix, self.roleNames())
"""
class MainWindow(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.g_groupe = gGroupe()
engine.rootContext().setContextProperty('ModelGroupe', self.g_groupe)
engine.load('main.qml')
win = engine.rootObjects()[0]
#win.findChild(QObject, "txtLibelle").setProperty("text", 'toto')
print(win.findChild(QObject, "listview22").setCurrentIndex(2))
a = win.findChild(QObject, "PersonModel")#.removeRows( 0, model.rowCount() )
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
# Création d'un objet QQmlContext pour communiquer avec le code QML
#ctx = engine.rootContext()
py_mainapp = MainWindow()
sys.exit(app.exec())
您有以下错误:
建议您创建一个QObject并添加qproperty来映射逻辑并导出到QML,因此如果您在python中修改QObject,它将在QML中修改它。你知道吗
主.py
主.qml
主_列表视图.qml
相关问题 更多 >
编程相关推荐