how can i send a python dictionary to a qml interface with a signal?

  • Last Update :
  • Techknowledgy :

The signature you have to use in the signal is QVariant:

class Test1(QObject):
   theSignal = Signal('QVariant')

@Slot(int)
def catchInt(self, caught):
   print("Caught: {0}".format(caught))
testDict = {
   "myAnswer": caught
}
self.theSignal.emit(testDict)

Suggestion : 2

This page was last edited on 5 June 2016, at 03:29.

def init(self, model = , brand = , year = 0, in_stock = False):
   QtCore.QObject.init(self)
self._model = model
self.brand = brand
self.year = year
self._in_stock = in_stock
def brand(self): return self._brand
def year(self): return self._year
def inStock(self): return self._in_stock
self._model = model
self.changed.emit()
self._brand = brand
self.changed.emit()
self._year = year
self.changed.emit()
self._in_stock = in_stock
self.changed.emit()

Suggestion : 3

 March 27, 2022     pyside, python, qml, qt     No comments   

widget.qml

import QtQuick 2.0
import QtQuick.Controls.Material 2.15
import QtQuick.Layouts 1.12

Item {
   id: root
   width: 1000
   height: 800

   signal displayValueChanged(string setpoint)

   GridLayout {
      columns: 3

      Repeater {
         id: repeater1

         model: mainWidget.model
         ColumnLayout {
            property int outerIndex: index
            Repeater {
               id: repeater2
               model: mainWidget.setpoints(outerIndex)
               ColumnLayout {
                  BasicContainer {
                     Component.onCompleted: {
                        //Signal called
                        displayValueChanged(inputText)

                     }
                  }
               }
            }
         }
      }
   }
}

BasicContainer.qml

    import QtQuick 2.12
    import QtQuick.Controls.Material 2.15
    import QtQuick.Layouts 1.12

    Item {

       id: basicContainerItem
       width: 300
       height: 60
       visible: true

       signal valueChanged()

       property alias inputText: containerInput.text

       Rectangle {
          id: rectangle
          width: parent.width

          ColumnLayout {
             TextField {
                id: containerInput
                visible: true
                placeholderText: qsTr("Text Field")
                text: "Default value"
                // Contended line
                //textColor: "#FF3333"        
                onAccepted: {
                   console.log(text)
                   basicContainerItem.valueChanged()
                }
             }

          }

       }
    }

Suggestion : 4

Importing Python modules from Qt Resources also works starting with QML API 1.3 using Qt.resolvedUrl() from within a QML file in Qt Resources. As an alternative, addImportPath('qrc:/') will add the root directory of the Qt Resources to Python’s module search path.,addImportPath() now also accepts qrc:/ URLs. This is useful if your Python files are embedded as Qt Resources, relative to your QML files (use Qt.resolvedUrl() from the QML file).,If you are using PyOtherSide in combination with an application binary compiled from C++ code with Qt Resources (see Qt Resource System), you can inspect and access the resources from Python. This example demonstrates the API by walking the whole resource tree, printing out directory names and file sizes:,Changed in version 1.1.0: addImportPath() will automatically strip a leading file:// from the path, so you can use Qt.resolvedUrl() without having to manually strip the leading file:// in QML.

import io.thp.pyotherside 1.5
import pyotherside
def image_provider(image_id, requested_size):
   ...
   return bytearray(pixels), (width, height), format
import pyotherside

def image_provider(image_id, requested_size):
   ...

   pyotherside.set_image_provider(image_provider)
import pyotherside
import os.path

def walk(root):
   for entry in pyotherside.qrc_list_dir(root):
   name = os.path.join(root, entry)
if pyotherside.qrc_is_dir(name):
   print('Directory:', name)
walk(name)
else:
   data = pyotherside.qrc_get_file_contents(name)
print('File:', name, 'has', len(data), 'bytes')

walk('/')
# Assume func will be called with a QObject as sole argument
def func(qobject):
   # Getting properties
print(qobject.x)

# Setting properties
qobject.x = 123

# Calling slots and dynamic functions
print(qobject.someFunction(123, 'b'))

# Returning a QObject reference to the caller
return qobject

Suggestion : 5

Although PyQt5 allows any Python callable to be used as a slot when connecting signals, it is sometimes necessary to explicitly mark a Python method as being a Qt slot and to provide a C++ signature for it. PyQt5 provides the pyqtSlot() function decorator to do this.,Connecting a signal to a decorated Python method also has the advantage of reducing the amount of memory used and is slightly faster.,It is also possible to connect signals by passing a slot as a keyword argument corresponding to the name of the signal when creating an object, or using the pyqtConfigure() method. For example the following three fragments are equivalent:,It is possible to pass any Python object as a signal argument by specifying PyQt_PyObject as the type of the argument in the signature. For example:

from PyQt5.QtCore
import QObject, pyqtSignal

class Foo(QObject):

   # This defines a signal called 'closed'
that takes no arguments.
closed = pyqtSignal()

# This defines a signal called 'rangeChanged'
that takes two
# integer arguments.
range_changed = pyqtSignal(int, int, name = 'rangeChanged')

# This defines a signal called 'valueChanged'
that has two overloads,
# one that takes an integer argument and one that takes a QString
# argument.Note that because we use a string to specify the type of
   # the QString argument then this code will run under Python v2 and v3.
valueChanged = pyqtSignal([int], ['QString'])
class Foo(QObject):

   # This will cause problems because each has the same C++signature.
valueChanged = pyqtSignal([dict], [list])
from PyQt5.QtCore
import QObject, pyqtSignal

class Foo(QObject):

   # Define a new signal called 'trigger'
that has no arguments.
trigger = pyqtSignal()

def connect_and_emit_trigger(self):
   # Connect the trigger signal to a slot.
self.trigger.connect(self.handle_trigger)

# Emit the signal.
self.trigger.emit()

def handle_trigger(self):
   # Show that the slot has been called.

print "trigger signal received"
from PyQt5.QtWidgets
import QComboBox

class Bar(QComboBox):

   def connect_activated(self):
   # The PyQt5 documentation will define what the
default overload is.
# In this
case it is the overload with the single integer argument.
self.activated.connect(self.handle_int)

# For non -
   default overloads we have to specify which we want to
# connect.In this
case the one with the single string argument.
#(Note that we could also explicitly specify the
   default
   if we # wanted to.)
self.activated[str].connect(self.handle_string)

def handle_int(self, index):
   print "activated signal passed integer", index

def handle_string(self, text):
   print "activated signal passed QString", text
act = QAction("Action", self)
act.triggered.connect(self.on_triggered)

act = QAction("Action", self, triggered = self.on_triggered)

act = QAction("Action", self)
act.pyqtConfigure(triggered = self.on_triggered)
from PyQt5.QtCore
import QObject, pyqtSlot

class Foo(QObject):

   @pyqtSlot()
def foo(self):
   ""
" C++: void foo() "
""

@pyqtSlot(int, str)
def foo(self, arg1, arg2):
   ""
" C++: void foo(int, QString) "
""

@pyqtSlot(int, name = 'bar')
def foo(self, arg1):
   ""
" C++: void bar(int) "
""

@pyqtSlot(int, result = int)
def foo(self, arg1):
   ""
" C++: int foo(int) "
""

@pyqtSlot(int, QObject)
def foo(self, arg1):
   ""
" C++: int foo(int, QObject *) "
""