-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_signals.py
59 lines (48 loc) · 1.94 KB
/
test_signals.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
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QVBoxLayout, QWidget
from PyQt6.QtCore import QRunnable, QObject, pyqtSignal, QThreadPool, QTimer
# Signal Emitter Object
class SignalEmitter(QObject):
completionsFetched = pyqtSignal(str)
# Worker Runnable
class CompletionWorker(QRunnable):
def __init__(self, code, signalEmitter):
super().__init__()
self.code = code
self.signalEmitter = signalEmitter
def run(self):
# Simulate some processing
completion = f"Completions for: {self.code}"
# Emit signal via the signalEmitter
self.signalEmitter.completionsFetched.emit(completion)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt6 Signal-Slot Example with Debounce")
layout = QVBoxLayout()
self.editor = QTextEdit()
layout.addWidget(self.editor)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
self.threadPool = QThreadPool()
self.debounceTimer = QTimer()
self.debounceTimer.setSingleShot(True)
self.debounceTimer.timeout.connect(self.processText)
self.editor.textChanged.connect(self.debounceTextChanged)
# Initialize the SignalEmitter
self.signalEmitter = SignalEmitter()
self.signalEmitter.completionsFetched.connect(self.onCompletionsFetched)
def debounceTextChanged(self):
self.debounceTimer.start(500) # Adjust debounce time as needed
def processText(self):
code = self.editor.toPlainText()
worker = CompletionWorker(code=code, signalEmitter=self.signalEmitter)
self.threadPool.start(worker)
def onCompletionsFetched(self, completions):
print("Completions fetched:", completions)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())