How show a glowing label in QMainWindow

Viewed 149

I'm trying to figure out how to use RectangularGlow to create a glowing label with PyQt5, but I don't know about QML, and I can't figure out how to do. Here is what I have so far: example.qml:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
import QtGraphicalEffects 1.12


Label {
    text: "Identifiant"
    
    width: 400
    height: 200

    Rectangle {
        id: background
        anchors.fill: parent
        color: "black"
    }

    RectangularGlow {
        id: effect
        anchors.fill: rect
        glowRadius: 10
        spread: 0.2
        color: "white"
        cornerRadius: rect.radius + glowRadius
    }

    Rectangle {
        id: rect
        color: "black"
        anchors.centerIn: parent
        width: Math.round(parent.width / 1.5)
        height: Math.round(parent.height / 2)
        radius: 25
    }
}

And the python code that uses it:

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import *

 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load('example.qml')
    label = engine.rootObjects()[0]
    
    win = QMainWindow()
    win.setCentralWidget(label)
    win.show()
    sys.exit(app.exec_())

But I get this error:

Traceback (most recent call last):
  File "test2.py", line 16, in <module>
    win.setCentralWidget(label)
TypeError: setCentralWidget(self, QWidget): argument 1 has unexpected type 'QObject'
2 Answers

If you want to use qml items in Qt Widgets then you must use a QQuickWidget (or QQuickView + QWidget::createWindowContainer()):

import os.path
import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtQuickWidgets import QQuickWidget

CURRENT_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)))


if __name__ == "__main__":
    app = QApplication(sys.argv)

    filename = os.path.join(CURRENT_DIR, "example.qml")

    qml_widget = QQuickWidget()
    qml_widget.setResizeMode(QQuickWidget.SizeRootObjectToView)
    qml_widget.setSource(QUrl.fromLocalFile(filename))

    win = QMainWindow()
    win.setCentralWidget(qml_widget)
    win.show()
    sys.exit(app.exec_())

you can have glow effect with :

import QtGraphicalEffects 1.12

also this is a label with glow effect :

Rectangle {
    width: 50
    clip: true
    height: 20
    color: "#592e2e2e"

    layer.enabled: true
    layer.effect: Glow {
        samples: 14
        color: "#592e2e2e"
        transparentBorder: true
    }

    Text {
        clip: true
        anchors.fill: parent
        wrapMode: Text.WrapAnywhere
        horizontalAlignment: Text.AlignHCenter
    }
}
Related