Skip to content

Commit

Permalink
add qt embed
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanguage committed Sep 18, 2022
1 parent 5a98ffd commit fbabc09
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
Binary file added docs/imgs/qt_app_embed.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions docs/qt_embed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Embeding generated window to a PyQt app

You can integrate oneFace generated Qt windows by embedding them in a Qt application:

```Python
# demo_qt_embed.py
import sys
from oneface.qt import gui
from oneface import one
from qtpy import QtWidgets

app = QtWidgets.QApplication([])


@gui
@one
def add(a: int, b: int):
res = a + b
print(res)

@gui
@one
def mul(a: int, b: int):
res = a * b
print(res)


main_window = QtWidgets.QWidget()
main_window.setWindowTitle("MyApp")
main_window.setFixedSize(200, 100)
layout = QtWidgets.QVBoxLayout(main_window)
layout.addWidget(QtWidgets.QLabel("Apps:"))
btn_open_add = QtWidgets.QPushButton("add")
btn_open_mul = QtWidgets.QPushButton("mul")
btn_open_add.clicked.connect(add.window.show)
btn_open_mul.clicked.connect(mul.window.show)
layout.addWidget(btn_open_add)
layout.addWidget(btn_open_mul)
main_window.show()

sys.exit(app.exec())
```

Run it:

![qt_embed](imgs/qt_app_embed.gif)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nav:
- Dash configs: dash_confs.md
- Wrap command line: wrap_cli.md
- Embedding:
- qt_embed.md
- dash_embed.md

markdown_extensions:
Expand Down
2 changes: 1 addition & 1 deletion oneface/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .core import one, Arg

__version__ = '0.1.7'
__version__ = '0.1.8'

__all__ = [one, Arg]
38 changes: 24 additions & 14 deletions oneface/qt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing as T
import functools
from qtpy import QtWidgets
from qtpy import QtCore
Expand Down Expand Up @@ -26,28 +27,38 @@ def gui(func=None, **kwargs):
return GUI(func, **kwargs)


def get_app():
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication([])
return app


class GUI():

type_to_widget_constructor = {}

def __init__(self, func, name=None, size=None,
run_once=True):
def __init__(
self,
func: T.Callable, name: T.Optional[str] = None,
size: T.Optional[T.List] = None,
run_once=True):
self.func = func
self.run_once = run_once
self.result = None
self.app = QtWidgets.QApplication([])
self.main_window = QtWidgets.QMainWindow()
self.app = get_app()
if name is not None:
name = name
self.name = name
elif hasattr(func, "name"):
name = func.name
self.name = func.name
else:
name = func.__name__
self.main_window.setWindowTitle(name)
self.name = func.__name__
self.window = QtWidgets.QWidget()
self.window._oneface_wrap = self
self.window.setWindowTitle(self.name)
if size:
self.main_window.setFixedSize(*size)
self.window.setFixedSize(*size)
self.arg_widgets = {}
self.window = QtWidgets.QWidget()
self.layout = QtWidgets.QVBoxLayout()
self.compose_ui()
self.connect_events()
Expand All @@ -58,7 +69,6 @@ def compose_ui(self):
self.layout.addWidget(self.run_btn)
self.terminal = QtWidgets.QTextEdit()
self.window.setLayout(self.layout)
self.main_window.setCentralWidget(self.window)

def compose_arg_widgets(self, layout: QtWidgets.QVBoxLayout):
arg_objs = get_func_argobjs(self.func)
Expand Down Expand Up @@ -86,9 +96,9 @@ def get_args(self):
def run_func(self):
kwargs = self.get_args()
if self.run_once:
self.main_window.hide()
self.window.hide()
self.result = self.func(**kwargs)
self.main_window.close()
self.window.close()
else:
thread = self.thread = QtCore.QThread()
worker = self.worker = Worker(self.func, kwargs)
Expand All @@ -106,7 +116,7 @@ def finish():
thread.finished.connect(finish)

def __call__(self):
self.main_window.show()
self.window.show()
self.app.exec()
return self.result

Expand Down

0 comments on commit fbabc09

Please sign in to comment.