Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Failed to connect signal in Python (using Pyside) to signal (work as slot) in QML, What is QVarient equivalent in PySide ?
Forum Updated to NodeBB v4.3 + New Features

Failed to connect signal in Python (using Pyside) to signal (work as slot) in QML, What is QVarient equivalent in PySide ?

Scheduled Pinned Locked Moved Solved Language Bindings
3 Posts 2 Posters 1.4k 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.
  • V Offline
    V Offline
    VoLinhTruc
    wrote on 22 Feb 2022, 19:37 last edited by VoLinhTruc
    #1

    Currently, I have a signal in QML file like this:

    signal fromThread (string rev_mess, var rev_data)
    	onFromThread:
    	{
    
    	}
    

    I need to create a correct Signal in python to connect to fromThread in QML file.
    I tried this:

    class Bico_QUIThread(QThread):
        toUI = Signal()
        def __init__(self):
                self.toUI.connect(self._engine.rootObjects()[0].fromThread)
    

    But what I got is runtime error :

    RuntimeError: Failed to connect signal toUI().
    

    I know the reason is the argument's type of toUI and fromThread not matched.
    toUI must be some thing like:

    toUI = Signal(str, QVariant)
    

    But QVarriant is no longer support in Pyside.

    So what other solutions I can do ?

    Thank you a lot in advance.

    Full code:
    Python code:

    import sys
    
    from PySide6.QtCore import Signal, Slot
    from PySide6.QtGui import QGuiApplication
    from PySide6.QtQml import QQmlApplicationEngine
    from PySide6.QtCore import QThread
    
    class Bico_QUIThread(QThread):
        toUI = Signal()
        def __init__(self):
            QThread.__init__(self)
            self.engine = QQmlApplicationEngine()
            self.engine.load("C:/Users/Truc/Desktop/Bico_QUIThread_Sample.qml")
            self.toUI.connect(self.engine.rootObjects()[0].fromThread)
    
    
    
    if __name__ == "__main__":
        app = QGuiApplication(sys.argv)
        thr = Bico_QUIThread()
        thr.start()
        sys.exit(app.exec())
    

    .

    QML code:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    
    Window {
    	id: window
    	objectName: "window"
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
    	signal fromThread(string rev_mess, var rev_data)
    	onFromThread:
    	{
    		if (rev_mess == "size")
    		{
    			button.height = rev_data.height
    			button.width = rev_data.width
    		}
    		else
    		{
    			console.log(rev_mess + " " + rev_data)
    		}
    	}
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Feb 2022, 19:56 last edited by
      #2

      Hi,

      Good question but sadly I don't have definitive answer to it. However, did you already try to use the Any type from the typing module ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VoLinhTruc
        wrote on 23 Feb 2022, 07:29 last edited by VoLinhTruc
        #3

        The problem was solved.
        Replace QVariant -> "QVariant"
        The correct definition of the signal in python code is:

        toUI = Signal(str, "QVariant")
        

        Refer: https://github.com/mottosso/Qt5.py/issues/5

        1 Reply Last reply
        1

        1/3

        22 Feb 2022, 19:37

        • Login

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