0

I'm pretty new to PyQt5 and QML integration. I've been searching a long time about that problem and I can't find a scenario close enough to my case.

The error I'm having is the following(I'm using PyCharm):

File "D:/PyCharmProjects/SimpleQML.py", line 13, in __init__
    self.win = self.root.findChild(QObject, "mainWindow")AttributeError: 'NoneType' object has no attribute 'findChild'

Here is my python code:

import sys
from PyQt5.QtCore import QUrl, QObject
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView

class SimpleQML(QQuickView):
    def __init__(self, parent=None):
    super(SimpleQML, self).__init__(parent)

        self.setSource(QUrl.fromLocalFile("D:/Qt/SimpleQML/SimpleQML.qml"))
        self.root = self.rootObject()
        self.win = self.root.findChild(QObject, "mainWindow")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = SimpleQML()
    win.setTitle("SimpleQML")
    win.setResizeMode(QQuickView.SizeRootObjectToView)
    win.show()
    sys.exit(app.exec_())

Here is my (really simple) QML code:

import QtQuick 2.6
import QtQuick.Window 2.2

Rectangle{
    id: mainWindow
    objectName: "mainWindow"
    visible: true
    width: 400
    height: 400
    color: "#323232"
}

And if you have any advice concerning the integration of QML with PyQt5 it's more than welcome.

Thank you in advance!

1 Answer 1

1

The error you get is valid since when you use the rootObject function you get the mainWindow rectangle, and when you do the search for your children you can not find any since you do not have children. To check it you can use:

print(self.root.objectName())

Output:

mainWindow

If you change the qml to:

import QtQuick 2.6
import QtQuick.Window 2.2

Item{
    Rectangle{
        id: mainWindow
        objectName: "mainWindow"
        visible: true
        width: 400
        height: 400
        color: "#323232"
    }
}

Then you get the mainWindow child.

Sign up to request clarification or add additional context in comments.

13 Comments

Hi! Thank you for you answer! Unfortunately even trying to "print(self.root.objectName())" doesn't work. 'NoneType' object has no attribute 'objectName' And the weird thing is that I have been using the code for a while and it was working, but not anymore. The most frustrating thing is that I gave it to a friend and it seems to be working! I have no idea about what is going on xD
Are you sure ?, you could share your code through drive, dropbox or similar
Try reinstalling pyqt.
Do you get the widget displayed?
That's all the code I have. Ok, I'll try reinstalling pyqt tomorrow then! I'll keep you posted.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.