【发布时间】:2018-02-12 07:08:56
【问题描述】:
附加的程序抛出这些错误:
QPixmap:在 GUI 线程之外使用像素图是不安全的
QObject::startTimer: 定时器不能从另一个线程启动
import sys
import PySide
import numpy as np
import pyqtgraph as pg
import threading
from PySide import QtGui, QtCore
from PySide.QtGui import *
from PySide.QtCore import *
from ui_mainWindow import Ui_MainWindow
#-------------------------------------------------------------------------------
# Main Window Class
# handle events and updates to the Qt user interface objects
#-------------------------------------------------------------------------------
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.setup_actions()
self.show()
def setup_actions(self):
self.startScopeButton.clicked.connect(self.start_stop_scope)
def start_stop_scope(self):
print("starting scope")
self.scope_display_thread()
def scope_display_thread(self):
threading.Thread(target=self._scope_display_thread).start()
def _scope_display_thread(self):
global data_ch1
data_ch1 = np.random.normal(0, 10, 1000)
self.graphicsView.plot(data_ch1,clear=True)
#-------------------------------------------------------------------------------
# Main Application Start Point
#-------------------------------------------------------------------------------
def main():
app = QApplication(sys.argv)
mainWin = MainWindow()
ret = app.exec_()
sys.exit( ret )
data_ch1 = []
main()
这是我正在尝试做的简化版本.. 即有一个线程接收和绘制数据。通过搜索论坛等。我从根本上知道问题是我必须从“主 Qt 事件线程”更新情节..但我不知道如何做到这一点。我的代码的几个信号槽排列看起来很有希望,但只会导致程序崩溃。
请对我放轻松,我是一个改过自新的 C# 人 ;-) 在 C# 或 Java 中,通常我实际上会在绘制方法覆盖中进行绘制,然后在加载新数据时强制应用程序重新绘制中。似乎 PyQtGraph.plot() 试图立即绘画?我可以异步添加数据然后告诉主线程去重绘场景吗??
谢谢!
【问题讨论】: