1

I have obtained a widget from a QtDesigner and converted .ui file to .py file by pyside. now I want that widget to organize a database and an apart threading.Thread (in same module) to open and read database and send to UDP. the fact that I know how to deal with all these apartly but when bringing together it is hard. should I use thread as a class inside my widget class which is:

def setupUi(self, Form):
...
def retranslateUi(self, Form):
...
if __name__ == "__main__":
...
Form.show()

and also can anyone give an example for running another thread with that widget?

Thanks in advance

mesutali
  • 1,655
  • 2
  • 12
  • 9
  • Here is some [documentation](http://qt-project.org/wiki/ThreadsEventsQObjects) on threads and an [example](http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/). – NoDataDumpNoContribution Jun 12 '14 at 11:07
  • I have looked up them @Trilarion. Thank you. is there any other way to run thread as a class with my widget class than your suggestion link (QObject) – mesutali Jun 12 '14 at 15:04
  • You can derive from QThread and implement the run class. It's fine and for some time was even the recommended way. There will be problems with event handling afaik but if you are happy with the result just just do it. Otherwise using a QObject and moving to a thread is not that much more work either. – NoDataDumpNoContribution Jun 12 '14 at 15:33
  • @Trilarion your example is written in qt-script that I am running my code in pyside(python3.3). sorry that I could not make myself understood. – mesutali Jun 12 '14 at 15:38
  • The syntax is almost the same but I also posted you a full example below. – NoDataDumpNoContribution Jun 12 '14 at 17:04

1 Answers1

2

I give you an example for using QThreads in PySide/PyQt to achieve multithreading and communication with other Widgets. I use it myself.

from PySide import QtCore

class Master(QtCore.QObject):

    command = QtCore.Signal(str)

    def __init__(self):
        super().__init__()

class Worker(QtCore.QObject):

    def __init__(self):
        super().__init__()

    def do_something(self, text):
        print('in thread {} message {}'.format(QtCore.QThread.currentThread(), text))

if __name__ == '__main__':

    app = QtCore.QCoreApplication([])

    # give us a thread and start it
    thread = QtCore.QThread()
    thread.start()

    # create a worker and move it to our extra thread
    worker = Worker()
    worker.moveToThread(thread)

    # create a master object and connect it to the worker
    master = Master()
    master.command.connect(worker.do_something)

    # call a method of the worker directly (will be executed in the actual thread)
    worker.do_something('in main thread')

    # communicate via signals, will execute the method now in the extra thread
    master.command.emit('in worker thread')

    # start the application and kill it after 1 second
    QtCore.QTimer.singleShot(1000, app.quit)
    app.exec_()

    # don't forget to terminate the extra thread
    thread.quit()
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104