Search code examples
pythonpyqtqmlpyqt5vispy

qml doesn't work with vispy


In the process of studying Qt and Qt, Quick encountered a very interesting problem. I wanted to add to my application a widget on which something would be rendered using an OpenGl. I found a small example using vispy and decided to try it. And then something very interesting is happening. The fact is that one of my widgets is written in QML, and when I launch my application, the widget with OpenGL worked. A black square appears instead of the QML-widget. Also in the log the following is written:

WARNING: QQuickWidget cannot be used as a native child widget. Consider setting Qt::AA_DontCreateNativeWidgetSiblings

Here my code:

import QtQuick 2.7
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0

Rectangle {
width: 200
height: 200
color: 'white'

Rectangle {
    id: lef_rec
    width: parent.width / 2
    height: parent.height
    color: "green"
}

Rectangle {
    width: parent.width / 2
    height: parent.height
    anchors.left: lef_rec.right
    color: "blue"
}
}

In Python:

self.qml_wdg = QQuickWidget()
self.qml_wdg.setSource(QtCore.QUrl("main.qml"))     
canvas = Canvas(keys='interactive', vsync=False).native
layout = QtWidgets.QVBoxLayout()
layout.addWidget(canvas)
layout.addWidget(self.qml_wdg)        
self.centralwidget.setLayout(layout)

Separately everything works, together there is this error. I'm wondering what this problem is?


Solution

  • You must place the attribute with setAttribute():

    {your QApplication}.setAttribute(QtCore.Qt.AA_DontCreateNativeWidgetSiblings)
    

    Complete code:

    import sys
    
    from PyQt5 import QtWidgets, QtCore, QtQuickWidgets
    from vispy.app import Canvas
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            QtWidgets.QMainWindow.__init__(self, parent=parent)
            self.centralwidget = QtWidgets.QWidget()
            self.setCentralWidget(self.centralwidget)
            self.qml_wdg = QtQuickWidgets.QQuickWidget()
            self.qml_wdg.setSource(QtCore.QUrl("main.qml"))
            canvas = Canvas(keys='interactive', vsync=False).native
            layout = QtWidgets.QVBoxLayout()
            layout.addWidget(canvas)
            layout.addWidget(self.qml_wdg)
            self.centralwidget.setLayout(layout)
    
    
    app = QtWidgets.QApplication(sys.argv)
    app.setAttribute(QtCore.Qt.AA_DontCreateNativeWidgetSiblings)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
    

    enter image description here