Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created November 16, 2017 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/1f7cf4b05db060286f60487baa0e6146 to your computer and use it in GitHub Desktop.
Save eyllanesc/1f7cf4b05db060286f60487baa0e6146 to your computer and use it in GitHub Desktop.
47339044
import sys
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
# This is our window from QtCreator
import mainwindow_auto
class DataCaptureThread(QThread):
def collectProcessData(self):
print ("Collecting Process Data")
def __init__(self, *args, **kwargs):
QThread.__init__(self, *args, **kwargs)
self.dataCollectionTimer = QTimer()
self.dataCollectionTimer.moveToThread(self)
self.dataCollectionTimer.timeout.connect(self.collectProcessData)
def run(self):
self.dataCollectionTimer.start(1000)
loop = QEventLoop()
loop.exec_()
class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # gets defined in the UI file
self.btnStart.clicked.connect(self.pressedStartBtn)
self.btnStop.clicked.connect(self.pressedStopBtn)
def pressedStartBtn(self):
self.lblAction.setText("STARTED")
self.dataCollectionThread = DataCaptureThread()
self.dataCollectionThread.start()
def pressedStopBtn(self):
self.lblAction.setText("STOPPED")
self.dataCollectionThread.terminate()
def main():
# a new app instance
app = QApplication(sys.argv)
form = MainWindow()
form.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow_auto.ui'
#
# Created by: PyQt5 UI code generator 5.9.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(102, 144)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.btnStart = QtWidgets.QPushButton(self.centralwidget)
self.btnStart.setObjectName("btnStart")
self.verticalLayout.addWidget(self.btnStart)
self.btnStop = QtWidgets.QPushButton(self.centralwidget)
self.btnStop.setObjectName("btnStop")
self.verticalLayout.addWidget(self.btnStop)
self.lblAction = QtWidgets.QLabel(self.centralwidget)
self.lblAction.setObjectName("lblAction")
self.verticalLayout.addWidget(self.lblAction)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 102, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btnStart.setText(_translate("MainWindow", "btnStart"))
self.btnStop.setText(_translate("MainWindow", "btnStop"))
self.lblAction.setText(_translate("MainWindow", "lblAction"))
@sinasartip
Copy link

Hey this code produces the following error:

Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt.
You must not let any exception whatsoever propagate through Qt code.
If that is not possible, in Qt 5 you must at least reimplement
QCoreApplication::notify() and catch all exceptions there.

QObject::killTimer: Timers cannot be stopped from another thread
QObject::~QObject: Timers cannot be stopped from another thread

The second error about the Qtimer is also an odd one. I tried self.timer.stop() in several places but all give an error about no being able to stop a timer outside its thread.
It seems you would need to call self.timer.stop in the run method, I haven't figured out a useful way doing that though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment