我有这个带有自定义main.py
的QAbstractTableModel
import sys
import signal
from PyQt6.QtGui import QGuiApplication
from PyQt6.QtQml import QQmlApplicationEngine
from PyQt6.QtCore import Qt, QAbstractTableModel
class InstalledPkgsModel(QAbstractTableModel):
def __init__(self, data):
super(InstalledPkgsModel, self).__init__()
self._data = data
def data(self, index, role):
if role == Qt.ItemDataRole.DisplayRole:
value = self._data[index.row()][index.column()]
return value
def rowCount(self, index):
return len(self._data)
def columnCount(self, index):
try:
return len(self._data[0])
# If there are no installed mods in the prefix
except IndexError:
return 1
# Make app respond to Ctrl-C
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.quit.connect(app.quit) # type: ignore
# Populate manage table view installed packages
data = [
["git", "", "common", "1.0"],
["distutils", "", "common", "1.0"],
["bsa", "", "common", "0.4"],
["nexus", "", "common", "1.0"],
["fallout_4", "", "common", "0.1"],
]
installed_pkgs_model = InstalledPkgsModel(data)
engine.rootContext().setContextProperty("installed_pkgs_model", installed_pkgs_model)
engine.load("main.qml")
sys.exit(app.exec())
我想设置一些东西,以便在使用此模型的TableView
中选择行。我现在有这个main.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
ApplicationWindow {
visible: true
width: 1000
height: 700
title: "Portmod"
TableView {
width: 1000
height: 700
id: installedPkgsTable
columnSpacing: 1
rowSpacing: 1
clip: true
model: installed_pkgs_model
selectionModel: ItemSelectionModel {
model: installed_pkgs_model
}
delegate: Rectangle {
implicitWidth: 300
implicitHeight: 50
color: selected ? "blue" : "lightgray"
required property bool selected
Text {
text: display
}
}
}
}
问题是,它不起作用。单击行不起任何作用,有时它会输出以下内容:
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::rowsAboutToBeRemoved(QModelIndex,int,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::columnsAboutToBeRemoved(QModelIndex,int,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::rowsAboutToBeInserted(QModelIndex,int,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::columnsAboutToBeInserted(QModelIndex,int,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::rowsMoved(QModelIndex,int,int,QModelIndex,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::columnsMoved(QModelIndex,int,int,QModelIndex,int)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)
qt.core.qobject.connect: QObject::disconnect: No such signal QObject::modelReset()
发布于 2022-08-02 05:09:44
似乎通过setContextProperty()
注册它有一些奇怪的行为。它也不是向QML公开Python / C++的推荐方法。
改为使用:
PySide6:
QtQml.qmlRegisterSingletonInstance(
QAbstractTableModel, "com.example.model", 1, 0, "PyModel", model
)
PyQt6:
qmlRegisterSingletonInstance(
"com.example.model", 1, 0, "InstalledPkgsModel", installed_pkgs_model
)
然后,在QML中,按照正常情况导入以下内容:
import com.example.model 1.0
像这样使用它:
PySide6:
model: PyModel
PyQt6:
model: InstalledPkgsModel
单击一行什么也不做。
如果您想要能够选择,可以使用SelectionRectangle
https://stackoverflow.com/questions/73187837
复制相似问题