Am Donnerstag 13 August 2009 08:58:32 schrieb Arnold Krille:
> [...]
> Are these long-running functions implemented in python? Otherwise they will
> release the GIL and your gui becomes snappy again.

Yes, they are in Python.

I think it works now, attached a simple script for demonstration. Comments are 
welcome ;)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2009 Lukas Hetzenecker <l...@gmx.at>

import random
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ThreadBase(QThread):
    def __init__(self,  parent=None):
        QThread.__init__(self)
        self.parent = parent

        self._mutex = QMutex()

        self._action = None
        self._argument = None

    def action(self,  action, argument=None):
        self._mutex.lock()
        self._action = action
        self._argument = argument
        self._mutex.unlock()

class Thread(ThreadBase):
    def __init__(self,  parent=None):
        ThreadBase.__init__(self)

    def run(self):
        print "Started Thread..."
        while True:
            self._mutex.lock()
            action = self._action
            argument = self._argument

            if action:
                if action == "hello":
                    print "Hello World!"
                elif action == "random":
                    print "Hello " + argument
                elif action == "quit":
                    print "Bye bye!"
                    self._action = None
                    self._argument = None
                    self._mutex.unlock()
                    self.quit()
                    break

                self._action = None
                self._argument = None

            self._mutex.unlock()

            qApp.processEvents()
            self.usleep(300)

class Widget(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        startBtn = QPushButton("Start")
        helloBtn = QPushButton("Say hello!")
        textBtn = QPushButton("Say random text!")
        endBtn = QPushButton("Quit Thread")

        self.thread = Thread()

        layout = QVBoxLayout(self)
        layout.addWidget(startBtn)
        layout.addWidget(helloBtn)
        layout.addWidget(textBtn)
        layout.addWidget(endBtn)

        self.setLayout(layout)

        self.connect(startBtn,  SIGNAL("clicked()"),  self.startThread)
        self.connect(helloBtn, SIGNAL("clicked()"),  self.sayHello)
        self.connect(textBtn, SIGNAL("clicked()"),  self.randomText)
        self.connect(endBtn, SIGNAL("clicked()"),  self.quitThread)

    def startThread(self):
        self.thread.start()

    def sayHello(self):
        self.thread.action("hello")

    def randomText(self):
        text = str().join( [ chr(random.randint(ord("A"), ord("z"))) for i in range(10) ] )
        self.thread.action("random", text)

    def quitThread(self):
        self.thread.action("quit")

app = QApplication([])
w = Widget()
w.show()
app.exec_()
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to