PyQt5 QML currentIndexChanged符号

2024-09-28 22:16:42 发布

您现在位置:Python中文网/ 问答频道 /正文

问题如下。

我有一个列表视图“主.qml“QML文件:

ListView {
    id: websiteListView
    orientation: ListView.Vertical
    flickableDirection: Flickable.VerticalFlick
    anchors.fill: parent
    model: websiteModel
    focus: true
    highlight: Rectangle { color: "lightsteelblue";}
    highlightFollowsCurrentItem: true
    objectName: "websiteListView"

    delegate: Component {
        Item {
            property variant itemData: model.modelData
            width: parent.width
            height: 20

            Row {
                id: row1
                spacing: 10
                anchors.fill: parent

                Text {
                    text: name
                    font.bold: true
                    anchors.verticalCenter: parent.verticalCenter
                }

                MouseArea {
                    id: websiteMouseArea
                    anchors.fill: parent
                    onClicked: {
                        websiteListView.currentIndex = index
                    }
                }
            }
        }
    }
}

我还拥有这个Python脚本:

^{pr2}$

以及负责信号处理的功能:

@pyqtSlot(int, int)
def __website_event_print(self, current, previous):

    print(current)
    print(previous)

上面显示的代码只是整个应用程序的摘录,但我相信其他代码行与此问题无关。

当我试图运行我的应用程序时发生了一个错误

TypeError: decorated slot has no signature compatible with currentIndexChanged()

我已经尝试了上述代码的大量变体,但似乎没有任何效果。我处理信号的方式正确吗?如果是,那么“currentIndexChanged”的签名是什么?


Tags: 代码idtruemodelcurrentwidthfillparent
1条回答
网友
1楼 · 发布于 2024-09-28 22:16:42

不建议在qml之外连接qml项,因为您只能按照代码显示的QObject获得它,而不能获得真正的对象,因为它是私有的,currentIndexChanged会通知发生了更改,但是没有参数将其传递给您得到的错误。在

一种可能的解决方案是创建一个通过setContextProperty()插入QML的对象,并在发出信号时调用函数:

.py:

class Helper(QObject):
    @pyqtSlot(int)
    def foo(self, index):
        print(index)

# ...

engine = QQmlApplicationEngine()
helper = Helper()
engine.rootContext().setContextProperty("helper", helper)
engine.load(QUrl.fromLocalFile("main.qml"))

.质量管理

^{pr2}$

在下面的link中有一个例子

相关问题 更多 >