Python 如何在QMainWindow中显示发光标签
我试图弄清楚如何使用RectangularGlow创建一个带有PyQt5的发光标签,但我不知道QML,也不知道如何做。以下是我到目前为止的情况: example.qml:Python 如何在QMainWindow中显示发光标签,python,pyqt5,qml,Python,Pyqt5,Qml,我试图弄清楚如何使用RectangularGlow创建一个带有PyQt5的发光标签,但我不知道QML,也不知道如何做。以下是我到目前为止的情况: example.qml: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Layouts 1.1 import QtGraphicalEffects 1.12 Label { text: "Identifiant" width:
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
}
}
以及使用它的python代码:
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_())
但我得到了这个错误:
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'
回溯(最近一次呼叫最后一次):
文件“test2.py”,第16行,在
win.setCentralWidget(标签)
TypeError:setCentralWidget(self,QWidget):参数1具有意外的类型“QObject”
您可以通过以下方式获得辉光效果:
import QtGraphicalEffects 1.12
此外,这是一个具有光晕效果的标签:
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
}
}
如果要在Qt小部件中使用qml项,则必须使用
QQuickWidget
(或QQuickView
+QWidget::createWindowContainer()
):
导入操作系统路径
导入系统
从PyQt5.QtCore导入QUrl
从PyQt5.QtWidgets导入QApplication,QMainWindow
从PyQt5.QtQuickWidgets导入QQuickWidget
CURRENT_DIR=os.path.join(os.path.dirname(os.path.realpath(u文件_u)))
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
filename=os.path.join(当前目录,“example.qml”)
qml_widget=QQuickWidget()
qml_widget.setResizeMode(QQuickWidget.sizerootObject视图)
qml_widget.setSource(QUrl.fromLocalFile(文件名))
win=QMainWindow()
win.setCentralWidget(qml_小部件)
win.show()
sys.exit(app.exec_())
您尚未描述该问题。你的代码有效吗?如果没有,具体是什么不起作用?对不起,我很忙,忘记添加执行此操作时遇到的错误。因此,您的问题与RectangularGlow
无关。您只需要知道如何从python加载任何qml文件?我不确定,我想可能是因为我尝试使用RectangularGlow
时出错了。谢谢。这一点以及eyllanesc的答案正在发挥作用。不过,我想知道使用文本
,还是使用标签
(我没有做到)更好?请查看以下内容: