1

I have test.qml file and in PyQt5 am using

self.view = QQuickView()
self.view.setResizeMode(QQuickView.SizeRootObjectToView)
self.view.setSource(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__),'test.qml')))

I have a button when it is clicked it overwrite the test.qml and call view again to read the test.qml file but it still display the old test.qml not the new one. Why?

Is it something with QQmlEngine clearComponentCache()

sample of the code:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
import  os

class Ui_MainWindow(object):

    def add(self):
        # call a function in a nother python file that will open test.qml and overwite it 
        # when i call the test.qml again it will show the old version not the new one 
        self.view.setSource(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__),'root.qml'))) 

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(420, 380, 113, 32))
        self.pushButton.setObjectName("pushButton")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(170, 50, 551, 311))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        vbox = QtWidgets.QVBoxLayout(self.frame)

        # loading the qml for the first time
        self.view = QQuickView()
        self.view.setResizeMode(QQuickView.SizeRootObjectToView)
        self.view.setSource(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__),'root.qml')))
        self.widget = QtWidgets.QWidget.createWindowContainer(self.view)
        vbox.addWidget(self.widget)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.pushButton.clicked.connect(self.add)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
   import sys
   app = QtWidgets.QApplication(sys.argv)
   MainWindow = QtWidgets.QMainWindow()
   ui = Ui_MainWindow()
   ui.setupUi(MainWindow)
   MainWindow.show()
   sys.exit(app.exec_())
1
  • the qml is a normal qml file i did not add anything to it
    – L..
    Apr 25, 2018 at 12:38

1 Answer 1

0

I have not worked with QQuickView yet but rather prefer QQmlApplicationEngine. With it, I used an approach similar to the one outlined here.

I guess you could add a method like this to your main window class:

def reload(self):
    self.view.engine().clearComponentCache()
    self.view.setSource(
        QUrl.fromLocalFile(
            os.path.join(os.path.dirname(__file__),'root.qml')))

The clearComponentCache() method causes the engine to discard any previously loaded QML documents. On the next load operation (e.g. by using setSource() on your view), the engine will hence be force to reload the file from disk.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.