我仍在掌握QT ...我制作了一个python文件和一个QML文件。 Python文件从通过UDP获取的数据中更新量规的值。
尽管这仅工作一次,但是第一个UDP数据包进来并更新了量规,但是当它获取下一个数据包时,尽管值更新了,但量规本身却没有。
QML
CircularGauge { id: circularGauge x: 30 y: 30 value: itt1value minimumValue: 0 maximumValue: 1200 tickmarksVisible: false style: CircularGaugeStyle { maximumValueAngle: 400 minimumValueAngle: 90 } }
Python:
def configureApplication(): # Set up the application window app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) view.setTitle("my title") # Load the QML file qml_file = os.path.join(os.path.dirname(__file__), "maingui.qml") view.setSource(QUrl.fromLocalFile(os.path.abspath(qml_file))) # load the slots into the QML file view.rootContext().setContextProperty("itt1value", 0) t = threading.Thread(target=receivedata, args=(view,)) t.start() # Show the window if view.status() == QQuickView.Error: sys.exit(-1) view.show() # execute and cleanup app.exec_() del view
在线程方法中,receivedata()我从UDP获取数据,对其进行处理,然后将其发送到仪表,如下所示:
view.rootContext().setContextProperty("itt1value", itt)
receivedata()具有上面的详细信息,其中有一个while循环,但是量表实际上仅更新一次。如果我在QML文件中放置一条语句以显示itt1value,则它始终具有正确的值,那么我是否需要放入一种方法来检测对该值的更改并重新绘制量规?
编辑:
我被要求提供Receivedata()的详细信息,因此我将其附加在这里:def receivedata(view):
print("Starting UDP server...")
UDP_IP = "192.168.0.14"
UDP_PORT = 49000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
olditt = 0
loopruns = 0 # for debugging
while True:
rawstring = sock.recv(1024)
hexarray = []
#lots of irrelevent formatting here, result is int(value)
itt = float(hextoint(value, olditt))
olditt = itt
itt = format(itt, '.3f')
current = str(loopruns) # for debugging
view.setTitle(current) # for debugging
view.rootContext().setContextProperty("itt1value", itt)
loopruns = loopruns + 1
print(itt)
我仍在掌握QT ...我制作了一个python文件和一个QML文件。 Python文件从通过UDP获取的数据中更新量规的值。尽管这只起作用一次...第一个UDP数据包...
您有以下错误: