【发布时间】:2020-06-22 18:09:13
【问题描述】:
我正在尝试制作用于控制伺服的 QML 表单。所以我使用 QT 创建器制作了一个 QML 表单并使用 PYQT 加载它。从 QML 表单中,我试图读取一个滑块值来控制伺服。每次我尝试移动滑块时,它都会显示:
AttributeError: 'NoneType' 对象没有属性 'value' 中止 (核心转储)
这是我的 pyqt 代码:
import os
import sys
from PyQt5 import QtCore, QtGui, QtQml
dir_path = os.path.dirname(os.path.realpath(__file__))
class MainWindow(QtQml.QQmlApplicationEngine):
def __init__(self):
super(QtQml.QQmlApplicationEngine, self).__init__()
self.load(QtCore.QUrl.fromLocalFile(os.path.join(dir_path, "QML-1.0.qml")))
self.rootContext().setContextProperty("MainWindow", self)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.run)
self.timer.start(10)
if self.rootObjects():
# Inherit items from the GUI
self.window = self.rootObjects()[0]
self.text = self.window.findChild(QtCore.QObject, "textField")
self.slider = self.window.findChild(QtCore.QObject, "slider")
def run(self):
print (type(self.slider))
pass
@QtCore.pyqtProperty(int)
def rangeValue(self):
x = self.slider.value()
print x
return 10
if __name__ == '__main__':
app = QtGui.QGuiApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
这是我的 qml 代码:
import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtQuick.Controls.Universal 2.0
import QtGraphicalEffects 1.0
ApplicationWindow {
id: root
width: 900
height: 300
opacity: 1
title: "window"
visible: true
//visibility: Window.FullScreen
visibility: Window.Maximized
Dial {
id: dial
x: 77
y: 60
width: 102
height: 103
wheelEnabled: true
}
Slider {
id: slider
x: 28
y: 220
value: 0.5
onValueChanged: MainWindow.rangeValue(value)
}
Label {
id: label
x: 64
y: 16
width: 128
height: 24
text: qsTr("Servo-1")
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
任何帮助将不胜感激。 谢谢
【问题讨论】: