Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将QTimer与QT';QMainWindow(QTimer与QTWidget一起工作,但不与QMainWindow一起工作)_Python_Python 3.x_Pyqt_Pyqt4_Pyqtgraph - Fatal编程技术网

Python 如何将QTimer与QT';QMainWindow(QTimer与QTWidget一起工作,但不与QMainWindow一起工作)

Python 如何将QTimer与QT';QMainWindow(QTimer与QTWidget一起工作,但不与QMainWindow一起工作),python,python-3.x,pyqt,pyqt4,pyqtgraph,Python,Python 3.x,Pyqt,Pyqt4,Pyqtgraph,我在QT设计器中创建了一个QMainWindow,其中包含一个GraphicsView对象,该对象已升级为pyQTGraph PlotWidget。我想使用QTimer事件来获取实时串行数据(Y),并用QTimer事件设置的1秒增量(X)来绘制它。然而,我遇到的问题是,当使用QT的QMainWindow作为主窗体时,我会得到错误“QObject::startTimer:QTimer只能用于以QThread启动的线程”,但是如果我使用QT的QWidget作为主窗体,一切都可以正常工作,没有错误 如

我在QT设计器中创建了一个QMainWindow,其中包含一个GraphicsView对象,该对象已升级为pyQTGraph PlotWidget。我想使用QTimer事件来获取实时串行数据(Y),并用QTimer事件设置的1秒增量(X)来绘制它。然而,我遇到的问题是,当使用QT的QMainWindow作为主窗体时,我会得到错误“QObject::startTimer:QTimer只能用于以QThread启动的线程”,但是如果我使用QT的QWidget作为主窗体,一切都可以正常工作,没有错误

如何将QTimer与QT的qmain窗口一起使用

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_())

任何帮助都将不胜感激

我建议将
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
输出:


我建议将
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
输出:


谢谢你eyllanesc,这非常有效,再次感谢你!谢谢你eyllanesc,工作得很好,再次谢谢你!