1

I'm using PyQt to integrate QtQuick into my Python app. I'm trying to figure out the best way to test my QtQuick Qml pages within a Python unittest framework. I'd like to pass a list of Qml files to some test function that makes sure no errors/exceptions exist for any of these files. Currently I'm trying to load a page into QQmlComponent and check for errors, but I haven't been able to get this working:

def test_qml(self):
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    rel = "/gui/QT/Page1.qml"
    c = QQmlComponent(engine, QUrl.fromLocalFile(SRC_PATH + os.path.normpath(rel)))
    print(c.errors())

Moreover from what I've read I think to get show errors with QQmlComponent I should catch a signal onStatusChange and then check so this seems like the wrong approach for me. What is the best way to go about trying to test qml pages in Python?

1 Answer 1

2

From what I understand you, you want to make the error message more readable. the errors() method returns a list of QQmlError, and this class has methods that give us accurate error information:

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtQml import *

type_error_toString = { 
    QtDebugMsg: "debug", 
    QtInfoMsg : "info", 
    QtWarningMsg : "wargning",
    QtCriticalMsg: "critical",
    QtFatalMsg: "fatal"
}

app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
path = "/path/of/item.qml"
c = QQmlComponent(engine, QUrl.fromLocalFile(path))

if c.isError():
    for error in c.errors():
        print(error.toString())
        print("type: {}, row : {}, column: {}, message: {}"
            .format(
                type_error_toString[error.messageType()],
                error.line(), 
                error.column(),
                error.description())
            )
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm when I try to create the QQmlComponent I get a segmentation fault so right now I'm not even able to get to c.isError. I know the path is correct and it looks like I'm calling the QQmlComponent correctly...any idea why this might be happening?
I'm sure the path is correct, I tried using the full path directly and got the same issue. The .qml I'd rather not put here, but it is very standard .qml, it imports QtQuick 2.9, QtQuick.Controls 2.2, and shows standard Qml elements like rectangles and buttons etc. It works without a problem when I actually run the program.
I'm running nosetests test_gui.py from the terminal to run this, still getting the seg fault. I'll update my question to include the imports I'm using. I was using my code with your updates but I will try your code directly.
It looks like once I remove my code from the test class and run it at the file level it no longer throws the seg fault.
@Stefan I recommend you first try the example that provides the answer and then just adapt it to your code, you could generate compatibility problems that have nothing to do with the question

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.