1
2#############################################################################
3##
4## Copyright (C) 2020 The Qt Company Ltd.
5## Contact: https://www.qt.io/licensing/
6##
7## This file is part of the Qt for Python examples of the Qt Toolkit.
8##
9## $QT_BEGIN_LICENSE:BSD$
10## You may use this file under the terms of the BSD license as follows:
11##
12## "Redistribution and use in source and binary forms, with or without
13## modification, are permitted provided that the following conditions are
14## met:
15##   * Redistributions of source code must retain the above copyright
16##     notice, this list of conditions and the following disclaimer.
17##   * Redistributions in binary form must reproduce the above copyright
18##     notice, this list of conditions and the following disclaimer in
19##     the documentation and/or other materials provided with the
20##     distribution.
21##   * Neither the name of The Qt Company Ltd nor the names of its
22##     contributors may be used to endorse or promote products derived
23##     from this software without specific prior written permission.
24##
25##
26## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37##
38## $QT_END_LICENSE$
39##
40#############################################################################
41
42import sys
43from PySide2.QtCore import QObject, QThread, Signal, Slot
44from PySide2.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
45
46
47# Create a basic window with a layout and a button
48class MainForm(QWidget):
49    def __init__(self):
50        QWidget.__init__(self)
51        self.setWindowTitle("My Form")
52        self.layout = QVBoxLayout()
53        self.button = QPushButton("Click me!")
54        self.button.clicked.connect(self.start_thread)
55        self.layout.addWidget(self.button)
56        self.setLayout(self.layout)
57
58    # Instantiate and start a new thread
59    def start_thread(self):
60        instanced_thread = WorkerThread(self)
61        instanced_thread.start()
62
63    # Create the Slots that will receive signals
64    @Slot(str)
65    def update_str_field(self, message):
66        print(message)
67
68    @Slot(int)
69    def update_int_field(self, value):
70        print(value)
71
72
73# Signals must inherit QObject
74class MySignals(QObject):
75    signal_str = Signal(str)
76    signal_int = Signal(int)
77
78
79# Create the Worker Thread
80class WorkerThread(QThread):
81    def __init__(self, parent=None):
82        QThread.__init__(self, parent)
83        # Instantiate signals and connect signals to the slots
84        self.signals = MySignals()
85        self.signals.signal_str.connect(parent.update_str_field)
86        self.signals.signal_int.connect(parent.update_int_field)
87
88    def run(self):
89        # Do something on the worker thread
90        a = 1 + 1
91        # Emit signals whenever you want
92        self.signals.signal_int.emit(a)
93        self.signals.signal_str.emit("This text comes to Main thread from our Worker thread.")
94
95
96if __name__ == "__main__":
97    app = QApplication(sys.argv)
98    window = MainForm()
99    window.show()
100    sys.exit(app.exec_())
101