我在QT Designer中创建了一个QMainWindow,其中一个GraphicsView对象已被提升为pyQTGraph PlotWidget。我想使用QTimer事件来获取实时串行数据(Y)并将其绘制为(X)为QTimer事件设置的1秒增量。但是,我遇到的问题是当使用QT的QMainWindow作为主要表单时,我得到错误“QObject :: startTimer:QTimer只能用于以QThread启动的线程”,但如果我使用QT的QWidget作为主要表单一切正常,没有错误。
如何将QTimer与QT的QMainWindow一起使用
QT4和QT Designer Python 3 PyQTGraph
以下是表格代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainPlotWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(735, 374)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.plot = PlotWidget(self.centralwidget)
self.plot.setGeometry(QtCore.QRect(30, 40, 681, 261))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.plot.sizePolicy().hasHeightForWidth())
self.plot.setSizePolicy(sizePolicy)
self.plot.setFrameShape(QtGui.QFrame.WinPanel)
self.plot.setFrameShadow(QtGui.QFrame.Sunken)
self.plot.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.plot.setObjectName(_fromUtf8("plot"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 735, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
from pyqtgraph import PlotWidget
以下是重现错误的简单测试程序:
from PyQt4.QtCore import QThread, SIGNAL, QSettings
import sys
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from MainPlotWindow import *
'''=====================================================================
M A I N G U I
====================================================================='''
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def PlotUpdate():
print("Hello")
timer = QtCore.QTimer()
timer.timeout.connect(PlotUpdate)
timer.start(1000) # 1 Second Refesh Rate
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
非常感谢任何帮助!
答案 0 :(得分:0)
我建议在窗口小部件中使用QTimer
,如下例所示:
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.PlotUpdate)
timer.start(100) # 1 Second Refesh Rate
self.curve = self.ui.plot.plot()
self.counter = 0
def PlotUpdate(self):
x = self.counter + np.arange(0, 10)
y = 10*np.sin(np.pi*x/10)
self.curve.setData(x, y)
self.counter += 1
输出: