Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. setContextProperty or Signal
Forum Updated to NodeBB v4.3 + New Features

setContextProperty or Signal

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 227 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.
  • C Offline
    C Offline
    christianc
    wrote on last edited by christianc
    #1

    Anyone have thoughts on the issues that could arise with using setContextProperty() to update UI elements instead of Signals?

    Signals require a python function/method & decorator, as well as Connection {} code in the qml file. It's much simpler to just call setContextProperty() again.

    The following examples both work, so Is it safe to use setContextProperty() in this case? Why use signals at all?

    Consider the following (i've paraphrased the code here):

    .py
    
    def update_status(msg):
        root.setContextProperty("statusText", msg)
    
    .qml
    
    Text {
        id: status
        text: qsTr(statusText)
    }
    

    This is a stark contrast to using a slot. (i've paraphrased the code here again)

    .py
    
    class signalClass():
    
        self.messageChanged= QtCore.Signal(str, arguments=['msg'])
    
        def update_status(msg):
            self.messageChanged.emit(msg)
    
    .qml
    
    Text {
        id: status
        text: qsTr(statusText)
        Connections {
            target: signalclass
            onMessageChanged: connectivityStatus.text = qsTr(msg)
        }
    }
    
    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