class Client(QWebEnginePage): def __init__(self, url): self.app = QGuiApplication(sys.argv) QWebEnginePage.__init__(self) self.loadFinished.connect(self.on_page_load) self.mainFrame().load(QUrl(url)) self.app.exec_() def on_page_load(self): self.app.quit()
def main(config): # Set default style to "fusion" # https://doc.qt.io/qt-5/qtquickcontrols2-styles.html#using-styles-in-qt-quick-controls-2 #os.environ.setdefault('QT_QUICK_CONTROLS_STYLE', 'fusion') qt_app = QGuiApplication(sys.argv) qt_app.setOrganizationName("EPFL") qt_app.setOrganizationDomain("ch") # Init QML qml_engine = QQmlApplicationEngine() # tell it the location of qml files qml_engine.addImportPath(str(DIR_RESOURCES)) # Register backend classes backend = LabelBackend() backend.load_config(Path(config)) backend.set_image_path(DIR_RESOURCES / 'images' / 'test.jpg') qml_engine.rootContext().setContextProperty('backend', backend) # QML loads image from the backend using an image provider qml_engine.addImageProvider('backend', backend.image_provider) # Load main window qml_engine.load(QUrl.fromLocalFile(str(DIR_RESOURCES / 'main.qml'))) if qml_engine.rootObjects(): exit_code = qt_app.exec_() del qml_engine sys.exit(exit_code) else: print('QML failed to load') sys.exit(1)
def main(): import sys app = QGuiApplication(sys.argv) w = PaintedWindow() w.create() w.show() sys.exit(app.exec_())
def run(config=None, dir=None): try: config, start_dir = get_config_and_start_dir(config=config, dir=dir) config = config or CONFIG_DEFAULT # Set default style to "fusion" # https://doc.qt.io/qt-5/qtquickcontrols2-styles.html#using-styles-in-qt-quick-controls-2 os.environ.setdefault('QT_QUICK_CONTROLS_STYLE', 'Fusion') qt_app = QGuiApplication(sys.argv) qt_app.setOrganizationName("EPFL") qt_app.setOrganizationDomain("ch") qt_app.setWindowIcon(QIcon(str(DIR_RESOURCES / 'label-grab-icon.svg'))) # Init QML qml_engine = QQmlApplicationEngine() # tell it the location of qml files qml_engine.addImportPath(str(DIR_RESOURCES)) # Register backend classes backend = LabelBackend() backend.load_config(Path(config)) backend.set_starting_directory(start_dir) backend.set_image_path(DIR_RESOURCES / 'images' / 'test.jpg') qml_engine.rootContext().setContextProperty('backend', backend) qtutils = QtUtils() qml_engine.rootContext().setContextProperty('utils', qtutils) # QML loads image from the backend using an image provider qml_engine.addImageProvider('backend', backend.image_provider) # Load main window qml_engine.load( QUrl.fromLocalFile(str(DIR_RESOURCES / 'qml' / 'main.qml'))) #qml_engine.load('qml/main.qml') if qml_engine.rootObjects(): exit_code = qt_app.exec_() del qml_engine sys.exit(exit_code) else: log.error('QML failed to load') sys.exit(1) except Exception as e: log.exception('Exception in main application') sys.exit(1)
def main(): import sys app = QGuiApplication(sys.argv) a = Window() a.setFramePosition(QPoint(10, 10)) a.setTitle("Window A") a.setObjectName(a.title()) a.setVisible(True) b = Window() b.setFramePosition(QPoint(100, 100)) b.setTitle("Window B") b.setObjectName(b.title()) b.setVisible(True) child = Window(b) child.setObjectName("ChildOfB") child.setVisible(True) windows = [] screens = app.screens() for screen in screens: if screen == app.primaryScreen(): continue window = Window(screen) geometry = window.geometry() geometry.moveCenter(screen.availableGeometry().center()) window.setGeometry(geometry) window.setVisible(True) window.setTitle(screen.name()) window.setObjectName(window.title()) windows.append(window) return app.exec() sys.exit(app.exec_())
from qtpy.QtQuick import QQuickView class MyClass(QObject): @Slot(int, result=str) # 声明为槽,输入参数为int类型,返回值为str类型 def returnValue(self, value): return str(value + 10) if __name__ == '__main__': path = 'src/demo1.qml' app = QGuiApplication([]) view = QQuickView() con = MyClass() context = view.rootContext() context.setContextProperty("con", con) view.engine().quit.connect(app.quit) view.setSource(QUrl(path)) view.show() app.exec_()
# Slot for summing two numbers @Slot(int, int) def sum(self, arg1, arg2): # Sum two arguments and emit a signal self.sumResult.emit(arg1 + arg2) # Slot for subtraction of two numbers @Slot(int, int) def sub(self, arg1, arg2): # Subtract arguments and emit a signal self.subResult.emit(arg1 - arg2) if __name__ == "__main__": import sys # Create an instance of the application app = QGuiApplication(sys.argv) # Create QML engine engine = QQmlApplicationEngine() # Create a calculator object calculator = Calculator() # And register it in the context of QML engine.rootContext().setContextProperty("calculator", calculator) # Load the qml file into the engine engine.load("main.qml") engine.quit.connect(app.quit) sys.exit(app.exec_())