Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have GUI application which uses qwebview to make web automation process through long loop so I used QThread to do this but I can't terminate the thread, my code is below

class Main(QMainWindow):
    def btStart(self):
        self.mythread = BrowserThread()
        self.connect(self.mythread, SIGNAL('loop()'), self.campaign_loop, Qt.AutoConnection)
        self.mythread.start()
    def btStop(self):
        self.mythread.terminate()
    def campaign_loop(self):
        loop goes here
class BrowserThread(QThread):
    def __init__(self):
        QThread.__init__(self)
    def run(self):
        self.emit(SIGNAL('loop()'))

this code is working fine in starting thread but fail to stop the loop and browser still running even if I call close event to it and it disappears from GUI

EDIT: it works on linux too, I tried this on raspberry pi 4 and it works fine

the point is to make the main loop in the " run " method because " terminate " function is stopping the loop in " run " not the thread its self here is a working example for this, but unfortunately it works on windows only

import sys
import time
from PySide.QtGui import *
from PySide.QtCore import *
class frmMain(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.btStart = QPushButton('Start')
        self.btStop = QPushButton('Stop')
        self.counter = QSpinBox()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.btStart)
        self.layout.addWidget(self.btStop)
        self.layout.addWidget(self.counter)
        self.setLayout(self.layout)
        self.btStart.clicked.connect(self.start_thread)
        self.btStop.clicked.connect(self.stop_thread)
    def stop_thread(self):
        self.th.stop()
    def loopfunction(self, x):
        self.counter.setValue(x)
    def start_thread(self):
        self.th = thread(2)
        #self.connect(self.th, SIGNAL('loop()'), lambda x=2: self.loopfunction(x), Qt.AutoConnection)
        self.th.loop.connect(self.loopfunction)
        self.th.setTerminationEnabled(True)
        self.th.start()
class thread(QThread):
    loop = Signal(object)
    def __init__(self, x):
        QThread.__init__(self)
        self.x = x
    def run(self):
        for i in range(100):
            self.x = i
            self.loop.emit(self.x)
            time.sleep(0.5)
    def stop(self):
        self.terminate()
app = QApplication(sys.argv)
win = frmMain()
win.show()
sys.exit(app.exec_())
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.