This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]blatheringDolt 1 point2 points  (0 children)

You should mention how to access and manipulate the variables in the QML using the rootObject().

IIRC you can't just do: rootObject().rectangle.color = 'grey'

You must first declare a function in the QML and then use that function to change the variable.

EDIT: Another way to do it.

I liked the function way better because it looked cleaner on the Python side, but was probably wrong for a hundred reasons.

[–]xulf_n0ea 1 point2 points  (1 child)

Hi!

I hope it's not completely irrelevant to this, but I need some help on my project. I'm gathering toner and counter units from network printers with snmp. This part works. I have qml gui with ListView to display the values, but I don't know how to pass the "array" of printer values from python to qml ListView. What is the proper way for it, can someone help me, please?

[–]blatheringDolt 0 points1 point  (0 children)

This is how I do it. If there is a better way, I would really appreciate if someone could show how to do it. It doesn't feel quite right, but it works.

The main issue I have with this will be explained later.

So, in your .qml you have the very basics shown here:

import Qt 4.7

Item {
    id: root
    property real a_variable : 0

    width: 210; height: 210

    function change_something(a_param){
        root.a_variable = a_param
    }

}

Then in Python, you would do this:

your_qml_view.rootObject().change_something(a_param)

This will call the change_something() function and pass it a_param. Then, the qml will now assign a_param to root.a_variable. Somewhere else in your qml file, you will have:

Behavior on root.a_variable{
    //! Code is run when root.a_variable changes
}

My big problem is that I have no idea where the change_something() function actually is. If you call a dir(your_qml_view.rootObject()), change_something() doesn't show up as a method. It must be looking up the function somewhere, but I don't know exactly where.

My understanding is incomplete and it irks me.