0

Okay, so I have this simple code that takes the property 'Text' of the object 'button_name' and modifies it. It does work , but only if the view(ApplicationWindow /engine) is active/focused and only if I move the mouse around. If the view is out of focus or the mouse isn't hovering over the application, the text doesn't Update on the screen. (tried it with other properties like 'visible' and it acts the same way.

import sys
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QObject, QUrl


if __name__ == '__main__':
    sys.argv += ['--style', 'material']
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine('basic.qml')
    button = engine.rootObjects()[0].findChild(QObject, "button_name")
    button.setProperty('text','New_Button_Text')
    sys.exit(app.exec_())  

And the basic.qml:

import QtQuick 2.0
import QtQuick.Controls 2.1
import QtQuick.Controls.Material 2.1

ApplicationWindow {
    visible: true
    width: 200
    height: 400
    Material.theme: Material.Light
    Material.accent: Material.Orange
    Column {
        anchors.centerIn: parent
        Button {
            objectName: "button_name"
            width: 200; height:50;
            font.capitalization: Font.MixedCase
            text: qsTr("Button Name")
            highlighted: true
            Material.background: Material.Orange

        }
    }
}

I did found "dataChanged()" but it doesn't seem to work for QQmlApplicationEngine,and I was looking into clearComponentCache() but that didn't do anything either.

I've also tried adding onTextChanged: console.log("Text has changed to:", text) to the Button and nothing gets printed(even after the text is updated in the view)

Also I can't access the property 'Material.background' of the Button(it returns NoneType) ,but I can access all of it's other properties.

aL_eX
  • 1,453
  • 2
  • 15
  • 30
HaR
  • 987
  • 7
  • 23
  • Why Button has 2 objectName? – eyllanesc Feb 06 '18 at 13:49
  • I have a curiosity, usually when the application is launched the focus is placed in the app, how have you tried or eliminated the focus of the app in your current example? – eyllanesc Feb 06 '18 at 13:53
  • @eyllanesc I've accidentally added it while playing around. Updated the code now. (the original code didn't have 2 objectName.) – HaR Feb 06 '18 at 14:07
  • @eyllanesc I have 'eliminated' the focus by opening a new window (like Chrome, a window from Explorer,etc..) – HaR Feb 06 '18 at 14:09
  • How strange, I just did the same thing and it's still working, I created an example more according to what you want with a QTimer and it works, check it: https://gist.github.com/eyllanesc/2af915dba55024f6c56cbb4cad8bbefe – eyllanesc Feb 06 '18 at 14:10
  • Also I recommend you not to make the changes of QML from python, the advisable if you want to manipulate the data from python is to create a class that inherits from QObject and export it to QML and there to make the changes. – eyllanesc Feb 06 '18 at 14:12
  • @eyllanesc,do you have an example on that? Something like this ? http://pyqt.sourceforge.net/Docs/PyQt5/qml.html#a-simple-example – HaR Feb 06 '18 at 14:19
  • 1
    Exactly, that's the recommended way to manipulate data – eyllanesc Feb 06 '18 at 14:20

0 Answers0