Message 1 of 1
PyQt5 with fusion 360 possible ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Ive been trying to get PyQt5 to work as a python add-in, but nothing seems to work.
running it on the main thread crashes fusion 360, and running it on a seperate thread also crashes it.
this is what im trying right now, but starting to realise it might not be possible to hold a thread for a gui myself without fusion 360 getting mad, am i doing something wrong ?
# Assuming you have not changed the general structure of the template no modification is needed in this file.
from . import commands
from .lib import fusion360utils as futil
import logging
import random
import sys
import time
from PyQt5.QtCore import QRunnable, Qt, QThreadPool
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
logging.basicConfig(format="%(message)s", level=logging.INFO)
# 1. Subclass QRunnable
class Runnable(QRunnable😞
def __init__(self, n😞
super().__init__()
self.n = n
def run(self😞
# Your long-running task goes here ...
for i in range(5😞
logging.info(f"Working in thread {self.n}, step {i + 1}/5")
time.sleep(random.randint(700, 2500) / 1000)
class Window(QMainWindow😞
def __init__(self, parent=None😞
super().__init__(parent)
self.setupUi()
def setupUi(self😞
self.setWindowTitle("QThreadPool + QRunnable")
self.resize(250, 150)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
# Create and connect widgets
self.label = QLabel("Hello, World!")
self.label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
countBtn = QPushButton("Click me!")
countBtn.clicked.connect(self.runTasks)
# Set the layout
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(countBtn)
self.centralWidget.setLayout(layout)
def runTasks(self😞
threadCount = QThreadPool.globalInstance().maxThreadCount()
self.label.setText(f"Running {threadCount} Threads")
pool = QThreadPool.globalInstance()
for i in range(threadCount😞
# 2. Instantiate the subclass of QRunnable
runnable = Runnable(i)
# 3. Call start()
pool.start(runnable)
def run(context😞
try:
# This will run the start function in each of your commands as defined in commands/__init__.py
commands.start()
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
except:
futil.handle_error('run')
def stop(context😞
try:
# Remove all of the event handlers your app has created
futil.clear_handlers()
# This will run the start function in each of your commands as defined in commands/__init__.py
commands.stop()
except:
futil.handle_error('stop')