在现有的Qt窗口中嵌入PyQtGraph图

时间:2017-05-09 11:20:33

标签: python qt matplotlib pyqtgraph

我正在尝试将PyQtGraphs嵌入到现有的Qt窗口中(使用Qt Designer设计并转换为.py文件)。我无法将PyQt GraphicLayoutView正确地调整到Qt组,而Qt组也在Tab中。

我之前曾设法使用MatPlotLib让图表工作,但是一旦数据集变大,它们的更新速度太慢,所以我决定切换到几个帖子建议的PyQtGraph。

我不确定是否正确嵌入图表,或者是否需要调整GrahpicLayoutView的大小以填充该组。

感谢任何帮助,附上代码和图片:

Image showing how the graphicsView doesnt size to fit the group

emgWindow_ui.py

# Form implementation generated from reading ui file 'emgWindow.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
import pyqtgraph as pg
import numpy as np

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):
        global win,curve
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1239, 796)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        self.centralWidget = QtGui.QWidget(MainWindow)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.centralWidget.sizePolicy().hasHeightForWidth())
        self.centralWidget.setSizePolicy(sizePolicy)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.gridLayout = QtGui.QGridLayout(self.centralWidget)
        self.gridLayout.setMargin(11)
        self.gridLayout.setSpacing(6)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.tabWidget = QtGui.QTabWidget(self.centralWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.tabWidget.sizePolicy().hasHeightForWidth())
        self.tabWidget.setSizePolicy(sizePolicy)
        self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
        self.recordTab = QtGui.QWidget()
        self.recordTab.setObjectName(_fromUtf8("recordTab"))
        self.dirFrame = QtGui.QFrame(self.recordTab)
        self.dirFrame.setGeometry(QtCore.QRect(10, 640, 711, 51))
        self.dirFrame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.dirFrame.setFrameShadow(QtGui.QFrame.Raised)
        self.dirFrame.setObjectName(_fromUtf8("dirFrame"))
        self.dirTextBox = QtGui.QTextEdit(self.dirFrame)
        self.dirTextBox.setGeometry(QtCore.QRect(10, 10, 511, 31))
        self.dirTextBox.setObjectName(_fromUtf8("dirTextBox"))
        self.recordBtn = QtGui.QPushButton(self.dirFrame)
        self.recordBtn.setGeometry(QtCore.QRect(530, 10, 81, 31))
        self.recordBtn.setObjectName(_fromUtf8("recordBtn"))
        self.stopBtn = QtGui.QPushButton(self.dirFrame)
        self.stopBtn.setGeometry(QtCore.QRect(620, 10, 81, 31))
        self.stopBtn.setObjectName(_fromUtf8("stopBtn"))
        self.trainingIMGBox = QtGui.QGroupBox(self.recordTab)
        self.trainingIMGBox.setGeometry(QtCore.QRect(730, 400, 481, 291))
        self.trainingIMGBox.setObjectName(_fromUtf8("trainingIMGBox"))
        self.IMUBox = QtGui.QGroupBox(self.recordTab)
        self.IMUBox.setGeometry(QtCore.QRect(730, 10, 481, 381))
        self.IMUBox.setObjectName(_fromUtf8("IMUBox"))
        self.EMGBox = QtGui.QGroupBox(self.recordTab)
        self.EMGBox.setGeometry(QtCore.QRect(10, 10, 711, 621))
        self.EMGBox.setObjectName(_fromUtf8("EMGBox"))
        self.tabWidget.addTab(self.recordTab, _fromUtf8(""))
        self.classifyTab = QtGui.QWidget()
        self.classifyTab.setObjectName(_fromUtf8("classifyTab"))
        self.frame_4 = QtGui.QFrame(self.classifyTab)
        self.frame_4.setGeometry(QtCore.QRect(280, 10, 921, 681))
        self.frame_4.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_4.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_4.setObjectName(_fromUtf8("frame_4"))
        self.featuresBox = QtGui.QGroupBox(self.classifyTab)
        self.featuresBox.setGeometry(QtCore.QRect(10, 10, 261, 191))
        self.featuresBox.setObjectName(_fromUtf8("featuresBox"))
        self.MAVCheck = QtGui.QCheckBox(self.featuresBox)
        self.MAVCheck.setGeometry(QtCore.QRect(10, 20, 141, 17))
        self.MAVCheck.setObjectName(_fromUtf8("MAVCheck"))
        self.RMSCheck = QtGui.QCheckBox(self.featuresBox)
        self.RMSCheck.setGeometry(QtCore.QRect(10, 40, 141, 17))
        self.RMSCheck.setObjectName(_fromUtf8("RMSCheck"))
        self.VARCheck = QtGui.QCheckBox(self.featuresBox)
        self.VARCheck.setGeometry(QtCore.QRect(10, 60, 131, 17))
        self.VARCheck.setObjectName(_fromUtf8("VARCheck"))
        self.SSICheck = QtGui.QCheckBox(self.featuresBox)
        self.SSICheck.setGeometry(QtCore.QRect(10, 80, 141, 17))
        self.SSICheck.setObjectName(_fromUtf8("SSICheck"))
        self.ZCCheck = QtGui.QCheckBox(self.featuresBox)
        self.ZCCheck.setGeometry(QtCore.QRect(10, 100, 131, 17))
        self.ZCCheck.setObjectName(_fromUtf8("ZCCheck"))
        self.WLCheck = QtGui.QCheckBox(self.featuresBox)
        self.WLCheck.setGeometry(QtCore.QRect(10, 120, 141, 17))
        self.WLCheck.setObjectName(_fromUtf8("WLCheck"))
        self.SSCCheck = QtGui.QCheckBox(self.featuresBox)
        self.SSCCheck.setGeometry(QtCore.QRect(10, 140, 141, 17))
        self.SSCCheck.setObjectName(_fromUtf8("SSCCheck"))
        self.WACheck = QtGui.QCheckBox(self.featuresBox)
        self.WACheck.setGeometry(QtCore.QRect(10, 160, 131, 17))
        self.WACheck.setObjectName(_fromUtf8("WACheck"))
        self.splitBox = QtGui.QGroupBox(self.classifyTab)
        self.splitBox.setGeometry(QtCore.QRect(10, 210, 261, 91))
        self.splitBox.setObjectName(_fromUtf8("splitBox"))
        self.trainSpin = QtGui.QSpinBox(self.splitBox)
        self.trainSpin.setGeometry(QtCore.QRect(110, 20, 42, 22))
        self.trainSpin.setMinimum(1)
        self.trainSpin.setMaximum(100)
        self.trainSpin.setSingleStep(5)
        self.trainSpin.setProperty("value", 70)
        self.trainSpin.setObjectName(_fromUtf8("trainSpin"))
        self.testSpin = QtGui.QSpinBox(self.splitBox)
        self.testSpin.setGeometry(QtCore.QRect(110, 60, 42, 22))
        self.testSpin.setMinimum(1)
        self.testSpin.setMaximum(100)
        self.testSpin.setSingleStep(5)
        self.testSpin.setProperty("value", 30)
        self.testSpin.setObjectName(_fromUtf8("testSpin"))
        self.label = QtGui.QLabel(self.splitBox)
        self.label.setGeometry(QtCore.QRect(10, 20, 91, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.splitBox)
        self.label_2.setGeometry(QtCore.QRect(10, 60, 91, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.splitBtn = QtGui.QPushButton(self.splitBox)
        self.splitBtn.setGeometry(QtCore.QRect(170, 20, 75, 61))
        self.splitBtn.setObjectName(_fromUtf8("splitBtn"))
        self.trainBtn = QtGui.QPushButton(self.classifyTab)
        self.trainBtn.setGeometry(QtCore.QRect(40, 310, 181, 41))
        self.trainBtn.setAutoFillBackground(False)
        self.trainBtn.setObjectName(_fromUtf8("trainBtn"))
        self.resultsBox = QtGui.QGroupBox(self.classifyTab)
        self.resultsBox.setGeometry(QtCore.QRect(10, 360, 261, 331))
        self.resultsBox.setObjectName(_fromUtf8("resultsBox"))
        self.resultsScroll = QtGui.QScrollArea(self.resultsBox)
        self.resultsScroll.setGeometry(QtCore.QRect(10, 20, 241, 301))
        self.resultsScroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.resultsScroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.resultsScroll.setWidgetResizable(True)
        self.resultsScroll.setObjectName(_fromUtf8("resultsScroll"))
        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 222, 299))
        self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
        self.resultsScroll.setWidget(self.scrollAreaWidgetContents)
        self.tabWidget.addTab(self.classifyTab, _fromUtf8(""))
        self.testTab = QtGui.QWidget()
        self.testTab.setObjectName(_fromUtf8("testTab"))
        self.detectBox = QtGui.QGroupBox(self.testTab)
        self.detectBox.setGeometry(QtCore.QRect(600, 10, 191, 681))
        self.detectBox.setObjectName(_fromUtf8("detectBox"))
        self.gestureTable = QtGui.QTableWidget(self.detectBox)
        self.gestureTable.setGeometry(QtCore.QRect(10, 20, 171, 651))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.gestureTable.setFont(font)
        self.gestureTable.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.gestureTable.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.gestureTable.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.gestureTable.setTextElideMode(QtCore.Qt.ElideMiddle)
        self.gestureTable.setRowCount(10)
        self.gestureTable.setColumnCount(1)
        self.gestureTable.setObjectName(_fromUtf8("gestureTable"))
        self.gestureTable.horizontalHeader().setVisible(False)
        self.gestureTable.horizontalHeader().setDefaultSectionSize(170)
        self.gestureTable.verticalHeader().setVisible(False)
        self.gestureTable.verticalHeader().setDefaultSectionSize(65)
        self.imgBox = QtGui.QFrame(self.testTab)
        self.imgBox.setGeometry(QtCore.QRect(10, 10, 581, 571))
        self.imgBox.setFrameShape(QtGui.QFrame.StyledPanel)
        self.imgBox.setFrameShadow(QtGui.QFrame.Raised)
        self.imgBox.setObjectName(_fromUtf8("imgBox"))
        self.gestureSignal = QtGui.QLabel(self.testTab)
        self.gestureSignal.setGeometry(QtCore.QRect(10, 590, 581, 21))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.gestureSignal.setFont(font)
        self.gestureSignal.setAlignment(QtCore.Qt.AlignCenter)
        self.gestureSignal.setObjectName(_fromUtf8("gestureSignal"))
        self.startBtn = QtGui.QPushButton(self.testTab)
        self.startBtn.setGeometry(QtCore.QRect(230, 630, 71, 51))
        self.startBtn.setObjectName(_fromUtf8("startBtn"))
        self.stopBtn_2 = QtGui.QPushButton(self.testTab)
        self.stopBtn_2.setGeometry(QtCore.QRect(310, 630, 75, 51))
        self.stopBtn_2.setObjectName(_fromUtf8("stopBtn_2"))
        self.currentBox = QtGui.QGroupBox(self.testTab)
        self.currentBox.setGeometry(QtCore.QRect(800, 10, 411, 681))
        self.currentBox.setObjectName(_fromUtf8("currentBox"))
        self.tabWidget.addTab(self.testTab, _fromUtf8(""))
        self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtGui.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 1239, 21))
        self.menuBar.setObjectName(_fromUtf8("menuBar"))
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtGui.QToolBar(MainWindow)
        self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtGui.QStatusBar(MainWindow)
        self.statusBar.setObjectName(_fromUtf8("statusBar"))
        MainWindow.setStatusBar(self.statusBar)

        self.win = pg.GraphicsLayoutWidget(self.EMGBox)
        #self.win = pg.GraphicsWindow()

        pg.setConfigOptions(antialias=True)
        self.p6 = self.win.addPlot(title="My Plot")
        self.curve = self.p6.plot(pen='r')

        data1 = np.random.normal(size=300)
        curve1 = self.p6.plot(data1)
        ptr1 = 0
        def update1():
            global data1, ptr1
            data1[:-1] = data1[1:]  # shift data in the array one sample left
                                    # (see also: np.roll)
            data1[-1] = np.random.normal()
            curve1.setData(data1)

            ptr1 += 1
            curve1.setData(data1)

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "EMG Gesute Classifier", None))
        self.recordBtn.setText(_translate("MainWindow", "Record", None))
        self.stopBtn.setText(_translate("MainWindow", "Stop", None))
        self.trainingIMGBox.setTitle(_translate("MainWindow", "Gesture", None))
        self.IMUBox.setTitle(_translate("MainWindow", "IMU Data", None))
        self.EMGBox.setTitle(_translate("MainWindow", "EMG Data", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.recordTab), _translate("MainWindow", "Record", None))
        self.featuresBox.setTitle(_translate("MainWindow", "Features", None))
        self.MAVCheck.setText(_translate("MainWindow", "Mean Absolute Value", None))
        self.RMSCheck.setText(_translate("MainWindow", "Root Mean Square", None))
        self.VARCheck.setText(_translate("MainWindow", "Variance", None))
        self.SSICheck.setText(_translate("MainWindow", "Simple Square Integral", None))
        self.ZCCheck.setText(_translate("MainWindow", "Zero Crossings", None))
        self.WLCheck.setText(_translate("MainWindow", "Wave Length", None))
        self.SSCCheck.setText(_translate("MainWindow", "Slope Sign Changes", None))
        self.WACheck.setText(_translate("MainWindow", "Willison Amplitude", None))
        self.splitBox.setTitle(_translate("MainWindow", "Split Data", None))
        self.label.setText(_translate("MainWindow", "Training Dataset:", None))
        self.label_2.setText(_translate("MainWindow", "Test Dataset:", None))
        self.splitBtn.setText(_translate("MainWindow", "Split", None))
        self.trainBtn.setText(_translate("MainWindow", "Train", None))
        self.resultsBox.setTitle(_translate("MainWindow", "Results", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.classifyTab), _translate("MainWindow", "Classify", None))
        self.detectBox.setTitle(_translate("MainWindow", "Gesture Detected", None))
        self.gestureSignal.setText(_translate("MainWindow", "SIGNAL", None))
        self.startBtn.setText(_translate("MainWindow", "Start", None))
        self.stopBtn_2.setText(_translate("MainWindow", "Stop", None))
        self.currentBox.setTitle(_translate("MainWindow", "Current", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.testTab), _translate("MainWindow", "Test", None))

全部从emgRun.py

执行
import sys

import numpy as np
import pyqtgraph as pg
from PyQt4 import QtCore, QtGui
from emgesture import EMG_Classify as emg

from emgWindow_ui import Ui_MainWindow

data = emg.dataset

class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

0 个答案:

没有答案