-
Notifications
You must be signed in to change notification settings - Fork 1
/
qtest.py
133 lines (115 loc) · 4.38 KB
/
qtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import math, random, sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self, imgQ, parent = None):
QWidget.__init__(self, parent)
self.thread = Worker()
label = QLabel(self.tr("Number of stars:"))
self.spinBox = QSpinBox()
self.spinBox.setMaximum(10000)
self.spinBox.setValue(100)
self.startButton = QPushButton(self.tr("&Start"))
self.viewer = QLabel()
self.viewer.setFixedSize(300, 300)
self.imgQ = imgQ
# self.thread.finished.connect(self.updateUi)
# self.thread.terminated.connect(self.updateUi)
# self.thread.output['QRect', 'QImage'].connect(self.addImage)
self.startButton.clicked.connect(self.makePicture)
layout = QGridLayout()
layout.addWidget(label, 0, 0)
layout.addWidget(self.spinBox, 0, 1)
layout.addWidget(self.startButton, 0, 2)
layout.addWidget(self.viewer, 1, 0, 1, 3)
self.setLayout(layout)
self.setWindowTitle(self.tr("Simple Threading Example"))
def makePicture(self):
self.spinBox.setReadOnly(True)
self.startButton.setEnabled(False)
for i in range(self.imgQ.qsize()-1):
self.imgQ.get()
img = self.imgQ.get()
height, width, channel = img.shape
bytesPerLine = 3 * width
qImg = QImage(img.data, width, height, bytesPerLine,
QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qImg)
self.viewer.setPixmap(pixmap)
# self.thread.render(self.viewer.size(), self.spinBox.value())
def addImage(self, rect, image):
pixmap = self.viewer.pixmap()
painter = QPainter()
painter.begin(pixmap)
painter.drawImage(rect, image)
painter.end()
self.viewer.update(rect)
def updateUi(self):
self.spinBox.setReadOnly(False)
self.startButton.setEnabled(True)
class Worker(QThread):
output = pyqtSignal(QRect, QImage)
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.exiting = False
self.size = QSize(0, 0)
self.stars = 0
self.path = QPainterPath()
angle = 2 * math.pi / 5
self.outerRadius = 20
self.innerRadius = 8
self.path.moveTo(self.outerRadius, 0)
for step in range(1, 6):
self.path.lineTo(
self.innerRadius * math.cos((step - 0.5) * angle),
self.innerRadius * math.sin((step - 0.5) * angle)
)
self.path.lineTo(
self.outerRadius * math.cos(step * angle),
self.outerRadius * math.sin(step * angle)
)
self.path.closeSubpath()
def __del__(self):
self.exiting = True
self.wait()
def render(self, size, stars):
self.size = size
self.stars = stars
self.start()
def run(self):
# Note: This is never called directly. It is called by Qt once the
# thread environment has been set up.
random.seed()
n = self.stars
width = self.size.width()
height = self.size.height()
while not self.exiting and n > 0:
image = QImage(self.outerRadius * 2, self.outerRadius * 2,
QImage.Format_ARGB32)
image.fill(qRgba(0, 0, 0, 0))
x = random.randrange(0, width)
y = random.randrange(0, height)
angle = random.randrange(0, 360)
red = random.randrange(0, 256)
green = random.randrange(0, 256)
blue = random.randrange(0, 256)
alpha = random.randrange(0, 256)
painter = QPainter()
painter.begin(image)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
painter.setBrush(QColor(red, green, blue, alpha))
painter.translate(self.outerRadius, self.outerRadius)
painter.rotate(angle)
painter.drawPath(self.path)
painter.end()
self.output.emit(QRect(x - self.outerRadius, y - self.outerRadius,
self.outerRadius * 2, self.outerRadius * 2),
image)
n -= 1
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())