- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在网上搜索,但没有找到如何使用 python 在 qml 文件中创建的 ListView 中选择一行。我测试过,但每次都会出错。我刚开始qml,也许基础不好。所以我的问题是,使用我的代码,可以访问我的 ListView 吗?如果是,我如何在启动应用程序中选择第二项(例如)?
当我这样做时:
print(win.findChild(QObject, "listview22").setCurrentIndex(2)))
我有这条消息
AttributeError: 'QQuickItem' object has no attribute 'setCurrentIndex'
但是listview22是一个 ListView 而不是一个项目。
感谢您的帮助。
PS:我刚刚启动 qml,所以如果我的脚本不好请告诉我,然后我可以学习好的编程
main.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}
}
}
}
Main_ListView.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())
最佳答案
您有以下错误:
您不应该将 QML 对象导出到 python,因为它会给您带来许多像您遇到的错误,甚至更严重的错误,这是 C++ 中推荐的。
当尝试在 C++ 中使用 findChild (QObject, "listview22") 获取 ListView 时,只会获取一个 QObject,但 python 会尝试推断类型,为此它具有 Qt 的公共(public) API 作为限制,在您的 ListView 中,它不是 QListView,并且它的类型在 Python 或 C++ 中不存在,因为它是私有(private) API 的一部分,因此附近的类是 QQuickItem,因为 ListView 继承自该类。
建议创建一个 QObject 并添加映射逻辑并导出到 QML 的 qproperties,因此,如果您在 python 中修改 QObject,它也会在 QML 中修改它。
main.py
from PyQt5.QtCore import (
pyqtProperty,
pyqtSignal,
pyqtSlot,
QAbstractListModel,
QModelIndex,
QObject,
Qt,
QTimer,
)
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
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.sort(0, 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.beginResetModel()
self.liste = [
{"iid": 10, "libelle": "groupe10"},
{"iid": 11, "libelle": "groupe11"},
{"iid": 12, "libelle": "groupe12"},
]
self.endResetModel()
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 insert(self, iid=10, libelle="toto"):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
self.liste.append({"iid": iid, "libelle": libelle})
self.endInsertRows()
@pyqtSlot(int)
def onClickItem(self, iid):
print(iid)
class MainWindow(QObject):
currentIndexChanged = pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self._g_groupe = gGroupe(self)
self._current_index = 0
@pyqtProperty(QObject, constant=True)
def g_groups(self):
return self._g_groupe
@pyqtProperty(int, notify=currentIndexChanged)
def currentIndex(self):
return self._current_index
@currentIndex.setter
def currentIndex(self, index):
if self._current_index == index:
return
self._current_index = index
self.currentIndexChanged.emit(index)
if __name__ == "__main__":
import sys
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
main_window = MainWindow()
engine.rootContext().setContextProperty("main_window", main_window)
engine.load("main.qml")
if not engine.rootObjects():
sys.exit(app.exec_())
main_window.currentIndex = 2
sys.exit(app.exec())
main.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
Connections{
target: main_window
onCurrentIndexChanged: lstGroupe.currentIndex = main_window.currentIndex
}
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{
id:lstGroupe;
model: main_window.g_groups
}
}
Item {
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}
}
Main_ListView.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
Rectangle {
id: root
property alias model: listview22.model
property alias currentIndex: listview22.currentIndex
border.color: "red"
Layout.fillWidth: true
height:150
ListView {
id: listview22
model: root.model
anchors.topMargin: 10
anchors.bottomMargin: 10
anchors.leftMargin: 10
anchors.fill: parent
clip: true
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
listview22.model.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: {
listview22.model.insert(22, "aluis")
}
}
}
关于python - 如何在 ListView 中选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57523238/
我之前在论坛上发布过这个,但我想我不太清楚。我有一个侧边栏 ListView 和一个包含我的控件的主面板。我想在 oncreate() 中突出显示 listview 中的 Activity 项,因为每
这里有没有人知道我如何通过 onTap() 扩展 ListView 项(在本例中为卡片)的内容 - 以显示更多选项?您知道那里有可以提供帮助的酒吧吗? 我已经看过了,但我不知道要搜索什么?我相信你们都
我的 ListView 控件有问题。我希望我的奇数行和偶数行具有不同的颜色,但我想通过代码而不是 FXML 来实现。例如: 第一行 - 绿色 第二行 - 红色 第三行 - 绿色 第四行 - 红色 现在
我有一个 ListView 。我想让单元格背景透明。目前,我正在做以下事情: .list-cell { -fx-background-color: transparent; } 但是,细胞的颜
我想创建一个几乎无限的元素列表,但我想将列表的初始位置设置为某个特定元素。像这张图片: 其中索引 0 将是初始位置,并且此列表可能会也可能不会在两个方向上延伸很长。 我可以像这样创建我的元素: W
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
开发人员。 我尝试在水平方向拉伸(stretch) ListView 项目。我确实重置了 ItemContainerStyle ,并且水平对齐是拉伸(stretch)的。这是 MainPage.xam
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
我想问一下: 我有一个预定义顺序的数组。 var order=new Array(); order[0]="Tesco"; order[1]="Interspar"; order[2]="
我希望创建以下内容:当到达内部 Listview 的顶部或底部时,我想继续在顶部 Listview 中滚动。见动图: Gif of what I got so far 一个选项是在到达底部时将内部 L
我正在尝试在 ajax 发布后刷新 jQuery 移动 ListView ,我一直在尝试使用 .trigger("create") 来执行此操作,如下所示: Most Played
我真的不明白是什么导致了错误我检查了文档,这里有一个非常相似的例子是我的 views.py,我使用的应用程序下的 urls.py 包含,以及模板 View .py class SchoolListVi
我有这个父布局 parent_listview 的列表项具有这种布局 child_listview 的项目有这个布局
我在单击列表项时获取 listview 项 时遇到问题。我得到了 simple listview(Arrayadapter) 的 listview item,但我遇到了 custom listview
我有一个工作正常的 DropdownMenu,但我需要一个 ListView,但我无法转换它。 DropdownMenu 如下所示: // Create the List of devices t
我想实现一个可滚动(垂直)且其中包含元素的屏幕。所以我把它放在 listview.builder 上。问题是,其中一个元素是另一个水平滚动的 listview.builder。当我实现它时,horiz
帮助!我想不通! 为了帮助我学习 SwiftUI,我正在使用 ListView 制作一个简单的杂货 list 应用程序。点击列表中的每个项目时,有些按钮会变成绿色或红色。 在我向列表数组中添加太多项目
所以我知道我们可以等待 ListView 加载,然后显示它。但是,如果我们动态加载正在下载的图像,ListView 将加载但图像尚未加载,因此它们不会出现在 ListView 中。 可接受的等待 Li
我是 Android 的新手,正在努力提高编程技能。 在这里,我有自定义 ListView 适配器类,我曾在其中显示 ListView 项目,如 TextView 、图像等。 我需要做的是,我在 Li
So, what I want is to individually disable a button for the concerned item in the ListView when clic
我是一名优秀的程序员,十分优秀!