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. How to emit Signal from nested QML page (by Loader) to python
Forum Updated to NodeBB v4.3 + New Features

How to emit Signal from nested QML page (by Loader) to python

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qtquicksignal & slot
3 Posts 3 Posters 814 Views
  • 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.
  • W Offline
    W Offline
    Witc
    wrote on last edited by
    #1

    Dear, in my QML/python app I can emit signal from main.qml to the python code. But now In main.qml I added StackLayout for loading another page1.qml. In that page1.qml is button, now I want to emit signal from this button to the python.

    I use this simple method for emit signals from main.qml file to the python: But I do not know how to emit it from "nested" page1.qml to the python. What should I modify? This code works when emits from main.qml

    main.py

    from PySide2.QtCore import QObject, QUrl, Slot, Signal, Qt
    from PySide2.QtGui import QGuiApplication
    from PySide2.QtQml import QQmlApplicationEngine
    
    class Foo(QObject):
        @Slot(str)
        def test_slot(self, input_string : str):
            print(input_string)
    
    if __name__ == "__main__":
        import os
        import sys
    
        app = QGuiApplication()
        foo = Foo()
        engine = QQmlApplicationEngine()
        
        #CHANGES: line excluded engine.rootContext().setContextProperty("foo", foo)
        
        qml_file = "main.qml"
        current_dir = os.path.dirname(os.path.realpath(__file__))
        filename = os.path.join(current_dir, qml_file)
        engine.load(QUrl.fromLocalFile(filename))
        if not engine.rootObjects():
            sys.exit(-1)
        
        #CHANGES: connect QML signal to Python slot
        engine.rootObjects()[0].test_signal.connect(foo.test_slot, type=Qt.ConnectionType.QueuedConnection)
        
        sys.exit(app.exec_())
    

    main.qml

    import QtQuick 2.13
    import QtQuick.Controls 2.13
    
    ApplicationWindow {
        visible: true
        
        //CHANGES: declare signal
        signal test_signal(string input_string)
    
        Button {
            anchors.centerIn: parent
            text: "Example"
    
            //CHANGES: emit signal
            onClicked: test_signal("Test string")
        }
    }
    
    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      I guess if you create a function in your main qml in which this signal is sent out. And call this func from page1. This will work. Other people may have better ideas. If you have signals at different layers, you may want to create a global model and call the model to emit these signals.

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        You don't.

        C++ shouldn't connect to QML signals.

        Expose a slot/invokable function in a Python object that is accessible from QML and call that slot in QML.

        1 Reply Last reply
        1

        • Login

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