pyqt6-patterns
Best practices and patterns for building robust PyQt6 desktop applications
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Best practices and patterns for building robust PyQt6 desktop applications
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | pyqt6-patterns |
| description | Best practices and patterns for building robust PyQt6 desktop applications |
Guide for building Desktop applications with PyQt6, focusing on architecture, threading, and user experience.
Use a model that separates UI and Logic:
main.py# Imports
from PyQt6.QtWidgets import ...
from PyQt6.QtCore import QThread, pyqtSignal
# 1. Background Thread Class
class WorkerThread(QThread):
progress = pyqtSignal(int, str)
finished = pyqtSignal(object)
error = pyqtSignal(str)
def run(self):
try:
# Heavy task here
pass
except Exception as e:
self.error.emit(str(e))
# 2. Main Window Class
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setup_ui()
def start_task(self):
self.thread = WorkerThread(...)
self.thread.progress.connect(self.on_progress)
self.thread.finished.connect(self.on_finished)
self.thread.start()
Inviolable Rule: NEVER run heavy tasks on the Main Thread.
Use QThread:
QThread.pyqtSignal) to communicate back to Main Thread.run() method.self.thread) in MainWindow.start().Always use Layouts for responsive UI:
QMainWindow
└── CentralWidget (QWidget)
└── QVBoxLayout
├── QGroupBox ("Input")
│ └── QFormLayout
├── QGroupBox ("Settings")
│ └── QVBoxLayout
└── QGroupBox ("Actions")
└── QHBoxLayout
Use Fusion style for a clean cross-platform look:
app = QApplication(sys.argv)
app.setStyle("Fusion")
In Worker Thread, always use try-except and emit error signal:
def run(self):
try:
# Dangerous code
do_work()
except Exception as e:
self.error.emit(str(e)) # Send error to UI
```
In UI, listen for signal and show MessageBox:
```python
def on_error(self, message):
self.btn_start.setEnabled(True) # Re-enable UI
QMessageBox.critical(self, "Error", message)
path = QFileDialog.getExistingDirectory(self, "Select Folder")
if path:
self.input_dir = path
self.label.setText(path)
progressBar.setRange(0, 0)emit(percent) from thread -> progressBar.setValue(percent)Use QTextEdit readonly to display realtime logs:
self.log_text = QTextEdit()
self.log_text.setReadOnly(True)
# Append log signal
self.log_text.append(message)
When activating this skill (generating code), print: "🎯 [SKILL ACTIVATED] pyqt6-patterns v1.0.0" "📋 Parameters:" " - Component: [MainWindow|WorkerThread|Dialog]" " - Pattern Applied: [Threading|Layout|Signal-Slot]"
Before applying major architectural changes: "I'm implementing the [Pattern Name] pattern for [Component]. This will structure the code as [Description]. Proceed?"
Best practices for robust Excel data processing with Pandas and OpenPyXL
Git workflow patterns, commit conventions, and edge case handling for Antigravity projects
Guide for packaging Python apps with PyInstaller for Windows and macOS