Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Python (PyQt5) and signals

Python (PyQt5) and signals

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 966 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JasonS
    wrote on last edited by JasonS
    #1

    I'm trying to get signals working from Python and need some help.

    I have one main QML file (main.qml) with an application window and then am loading a second QML file with a pane via StackView

    main.qml

    import QtQuick 2.2
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.0
    import QtQuick.Controls.Material 2.0
    
    ApplicationWindow{
        id: window
        width: 800
        height: 480
        visible: true
        title: "Demo"
    
        StackView {
            id: stackView
            anchors.fill:   parent
            initialItem: Qt.resolvedUrl("pages/Page1.qml")
        }
    }
    

    page1.qml

    import QtQuick 2.6
    import QtQuick.Extras 1.4
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Controls 2.0
    
    Pane{
        id: pane1
        width: 800
        height: 480
    
        property var testString: "Test works"
    
        function test(t)
        {
            testString = t
        }
    
        Text {
            id: testText
            x: 11
            y: 62
            text:  testString
    
            font.pixelSize: 150
        }
    }
    

    main.py

    import os
    import sys
    import threading
    
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtQml import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtQuick import *
    
    class Test(QObject):
        done = pyqtSignal(int)
        _running = False
    
        def __init__(self):
            QObject.__init__(self)
            self._running = False
            print('__init__')
    
        def _Test(self):
            print('_Test')
            self._running = False
            self.done.emit('Test worked')
    
        @pyqtSlot()
        def doTest(self):
            if not self.running:
                print('doTest')
                self._running = True
                thread = threading.Thread(target=self._Test)
                thread.start()
    
    def main():
    
        app = QGuiApplication(sys.argv)
    
        engine = QQmlApplicationEngine()
    
        test = Test()
        engine.rootContext().setContextProperty('test', test)
    
        engine.load(QUrl("main.qml"))
        root = engine.rootObjects()[0]
        test.done.connect(root.test)
        app.exec()
    
    if __name__ == "__main__":
        main()
    

    The error I get is:
    init
    Traceback (most recent call last):
    File "main.py", line 48, in <module>
    main()
    File "main.py", line 44, in main
    test.done.connect(root.test)
    AttributeError: 'QQuickWindow' object has no attribute 'test'

    I understand why, but I don't know how to correct it. How do I get the test function so I can connect to it when it's in a child of mail.qml?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved