[PyQt] QML test with component defined in Python

Cody Scott cody at perspexis.com
Thu Jan 19 14:10:17 GMT 2017


I'm trying to write a test in QML using QtTest that tests a QML component
defined in Python using QtQml.qmlRegisterType().

It appears that the PyQt5==5.7.1 wheel is missing the correct files for
QtTest.

Here is an example of what I am trying to do

This gist is the same as the attached file.
https://gist.github.com/Siecje/c93f4faf15baa89d4fd0c9ff86d22a6a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170119/9e4715f3/attachment.html>
-------------- next part --------------
#!/usr/bin/env python
import sys

from PyQt5 import Qt, QtCore, QtGui, QtQml, QtWidgets


class OutputType(QtCore.QObject):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.output = ""

    @QtCore.pyqtSlot()
    def ChangeOutput(self):
        self.output = "Hello"

QML = b"""
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtTest 1.1

import OutputType 1.0

Item {
    width: 800
    height: 600
    visible: true

    OutputType {
        id: outputType
    }

    Text {
        id: txt
        text: outputType.output
    }
    Button {
        id: btn
        onClicked: {
            outputType.ChangeOutput()
        }
    }
    TestCase {
      name: "MouseTests"
      function test_clicked(){
          compare(txt.text, "")
          btn.clicked()
          compare(txt.text, "Hello")
      }
    }
}
"""

app = QtWidgets.QApplication(sys.argv + ["-nograb"])
QtQml.qmlRegisterType(OutputType, 'OutputType', 1, 0, 'OutputType')
engine = QtQml.QQmlApplicationEngine()
engine.loadData(QML)
sys.exit(app.exec_())


More information about the PyQt mailing list