2017-07-06 140 views
0

我有QT Designer中創建的QMainWindow與已被提升爲pyQTGraph PlotWidget的GraphicsView對象。我想用一個QTimer事件來獲取實時串行數據(Y)與(X)繪製它是由QTimer事件設置1秒爲增量。不過,我遇到的問題是,使用QT的QMainWindow的爲主要表現形式,當我得到錯誤「的QObject :: startTimer所:開始的QThread QTimer只能與線程使用」,但如果我使用Qt的QWidget的爲主要形式一切工作正常,沒有錯誤。我怎樣才能QTimer與QT QMainWindow QTimer(QTimer與QTWidget但不QMainWindow)

如何使用QTimer與QT的QMainWindow的

QT4和Qt設計 的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

謝謝eyllanesc,完美地工作,再次感謝你! – Kinisi

回答

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 

輸出:

enter image description here