-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (56 loc) · 2.03 KB
/
main.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
import sys
from PySide6.QtCore import QObject, Slot, Property, Signal, QAbstractListModel, Qt
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
class FibonacciModel(QAbstractListModel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._data = []
def data(self, index, role):
if role == Qt.DisplayRole:
return self._data[index.row()]
def rowCount(self, index):
return len(self._data)
def setData(self, data):
self.beginResetModel()
self._data = data
self.endResetModel()
class AppBackend(QObject):
fibonacciOutputChanged = Signal()
def __init__(self, model):
super().__init__()
self.fibonacciModel = model
self._fibonacciOutput = ""
@Slot(int)
def generateFibonacci(self, n):
if n <= 0:
self.fibonacciModel.setData(["Please enter a positive integer."])
self._fibonacciOutput = "Please enter a positive integer."
else:
sequence = self.calculate_fibonacci(n)
self.fibonacciModel.setData(sequence)
self._fibonacciOutput = ", ".join(map(str, sequence))
self.fibonacciOutputChanged.emit()
def calculate_fibonacci(self, n):
if n == 1:
return [0]
elif n == 2:
return [0, 1]
sequence = [0, 1]
for i in range(2, n):
sequence.append(sequence[-1] + sequence[-2])
return sequence
@Property(str, notify=fibonacciOutputChanged)
def fibonacciOutput(self):
return self._fibonacciOutput
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
fibonacciModel = FibonacciModel()
backend = AppBackend(fibonacciModel)
engine = QQmlApplicationEngine()
engine.rootContext().setContextProperty("app", backend)
engine.rootContext().setContextProperty("fibonacciModel", fibonacciModel)
engine.load("main.qml")
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())