mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
WIP qapp POC
This commit is contained in:
0
bec_widgets/examples/qapp_custom/__init__.py
Normal file
0
bec_widgets/examples/qapp_custom/__init__.py
Normal file
33
bec_widgets/examples/qapp_custom/bec_qapp.py
Normal file
33
bec_widgets/examples/qapp_custom/bec_qapp.py
Normal file
@ -0,0 +1,33 @@
|
||||
from qtpy.QtWidgets import QApplication
|
||||
|
||||
|
||||
class BECQApplication(QApplication):
|
||||
def setup_bec_features(self):
|
||||
self.bec_props = {}
|
||||
self.is_bec_app = True
|
||||
print("[BECQApplication]: Features initialized.")
|
||||
|
||||
def inject_property(self, name, value):
|
||||
self.bec_props[name] = value
|
||||
print(f"[BECQApplication]: Injected property '{name}' = {value}")
|
||||
|
||||
|
||||
def upgrade_to_becqapp():
|
||||
app = QApplication.instance()
|
||||
if app is None:
|
||||
raise RuntimeError("No QApplication instance found!")
|
||||
|
||||
if getattr(app, "is_bec_app", False):
|
||||
print("[BECQApplication]: Already upgraded.")
|
||||
return app
|
||||
|
||||
# Only inject your explicitly defined Python methods
|
||||
methods_to_inject = ["setup_bec_features", "inject_property"]
|
||||
|
||||
for method_name in methods_to_inject:
|
||||
method = getattr(BECQApplication, method_name)
|
||||
setattr(app, method_name, method.__get__(app, QApplication))
|
||||
|
||||
app.setup_bec_features()
|
||||
print("[BECQApplication]: QApplication upgraded to BECQApplication.")
|
||||
return app
|
25
bec_widgets/examples/qapp_custom/bec_widget.py
Normal file
25
bec_widgets/examples/qapp_custom/bec_widget.py
Normal file
@ -0,0 +1,25 @@
|
||||
from qtpy.QtWidgets import QWidget, QLabel, QVBoxLayout, QApplication
|
||||
from bec_qapp import upgrade_to_becqapp
|
||||
|
||||
|
||||
class BECWidget(QWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
|
||||
# Upgrade qApp if necessary
|
||||
app = QApplication.instance()
|
||||
if not getattr(app, "is_bec_app", False):
|
||||
print("[BECWidget]: Upgrading QApplication instance to BECQApplication.")
|
||||
app = upgrade_to_becqapp()
|
||||
else:
|
||||
print("[BECWidget]: BECQApplication already active.")
|
||||
|
||||
app.inject_property("widget_initialized", True)
|
||||
|
||||
self.setup_ui()
|
||||
|
||||
def setup_ui(self):
|
||||
layout = QVBoxLayout()
|
||||
label = QLabel("BECWidget is running with BECQApplication features.")
|
||||
layout.addWidget(label)
|
||||
self.setLayout(layout)
|
44
bec_widgets/examples/qapp_custom/launch.py
Normal file
44
bec_widgets/examples/qapp_custom/launch.py
Normal file
@ -0,0 +1,44 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
from PySide6.QtWidgets import QMainWindow, QWidget, QVBoxLayout
|
||||
|
||||
# Set this early!
|
||||
# os.environ["PYSIDE_DISABLE_INTERNAL_QT_WARNINGS"] = "1"
|
||||
|
||||
from qtpy.QtWidgets import QApplication
|
||||
from bec_widget import BECWidget
|
||||
|
||||
|
||||
class DemoApp(QMainWindow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setWindowTitle("Demo Application")
|
||||
self.setGeometry(100, 100, 600, 400)
|
||||
|
||||
# Create an instance of BECWidget
|
||||
self.main_widget = QWidget(self)
|
||||
|
||||
self.setCentralWidget(self.main_widget)
|
||||
self.main_widget.layout = QVBoxLayout(self.main_widget)
|
||||
self.bec_widget_1 = BECWidget()
|
||||
self.bec_widget_2 = BECWidget()
|
||||
|
||||
# Set up the UI for the BECWidget
|
||||
self.bec_widget_1.setup_ui()
|
||||
self.bec_widget_2.setup_ui()
|
||||
|
||||
self.main_widget.layout.addWidget(self.bec_widget_1)
|
||||
self.main_widget.layout.addWidget(self.bec_widget_2)
|
||||
|
||||
|
||||
def main():
|
||||
app = QApplication(sys.argv)
|
||||
widget = DemoApp()
|
||||
widget.resize(400, 200)
|
||||
widget.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user