def start(self): """Display the GUI""" myApp = QApplication(sys.argv) sys.excepthook = lambda typ, val, tb: error_handler(typ, val, tb) myApp.setOrganizationDomain('mlox') myApp.setOrganizationName('mlox') icon_data: bytes = resource_manager.resource_string( "mlox.static", "mlox.ico") icon = QIcon() pixmap = QPixmap() pixmap.loadFromData(icon_data) icon.addPixmap(pixmap) myApp.setWindowIcon(icon) myEngine = QQmlApplicationEngine() # Need to set these before loading myEngine.rootContext().setContextProperty("python", self) myEngine.addImageProvider('static', PkgResourcesImageProvider()) qml: bytes = resource_manager.resource_string("mlox.static", "window.qml") myEngine.loadData(qml) # These two are hacks, because getting them in the __init__ and RAII working isn't self.debug_window = ScrollableDialog() self.clipboard = myApp.clipboard() self.analyze_loadorder() sys.exit(myApp.exec())
def main(args=sys.argv, qml_code=None): app = QApplication(args) app.setApplicationName('Batch resizer') app.setOrganizationName('BSC') qmlEngine = QQmlApplicationEngine() imd = ImageMagickDispatcher() qmlEngine.rootContext().setContextProperty("imageMagickDispatcher", imd) if qml_code: qmlEngine.loadData(qml_code.encode()) else: qmlEngine.load('resizer.qml') qmlEngine.quit.connect(app.quit) app.exec_()
@pyqtSlot(str, result=str) # 可以获取返回值 def testSlot(self, name): # 被qml调用的函数 self.resultView.append('我被主动调用: %s' % name) return str(len(name)) if __name__ == '__main__': try: QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) except: pass app = QApplication(sys.argv) # 测试界面 w = Window() w.resize(400, 400) w.show() w.move(400, 400) engine = QQmlApplicationEngine() # 提供一个沟通的对象_Window,必须是要继承QObject的类 engine.rootContext().setContextProperty('_Window', w) engine.objectCreated.connect( lambda obj, _: QMessageBox.critical(None, '错误', '运行失败,请检查') if not obj else 0) engine.loadData(QML.encode()) sys.exit(app.exec_())