我想知道如何使QApplication UI成为PyQt5项目上QML抽屉项的根窗口
原则上,我可以让它们一起工作,但抽屉会一直显示在不同的窗口上,而不是对根窗口应用相同的效果
举个例子,我试图将“Qt Quick Controls 2 - Gallery”的功能包含到项目中
有关更多详细信息,请查看以下代码:
# mainwindow.py
class MainWindow:
def __init__(self):
self.qtApplication = QtWidgets.QApplication([])
qtPbObj = QtWidgets.QPushButton
QtWidgets.QPushButton = hoverButton
self.mw = QtWidgets.QMainWindow()
self.mw.setWindowFlags(Qt.FramelessWindowHint | self.mw.windowFlags())
self.qtMainWindowUi = main_ui.Ui_MainWindow()
self.qtMainWindowUi.setupUi(self.mw)
QtWidgets.QPushButton = qtPbObj
...
engine = QQmlApplicationEngine()
context = engine.rootContext()
file = os.path.join(DIR_PATH, "main.qml")
url = QUrl.fromLocalFile(file)
engine.load(url)
engine.setParent(self.qtApplication.instance())
if not engine.rootObjects():
logger.error("No object could be loaded from sourced QML file")
self.qtApplication.exit(-1)
rootWindow = engine.rootObjects()[0]
使用的QML文件可以在here中找到
更新:
在项目中,在保持setupUi和无框架功能实现的同时,我希望QML抽屉功能仅在单击≡", 使其与主窗口中央窗口小部件重叠:
附上一些捕获以更好地解释实际状态:
实际用户界面:
理想溶液:
我认为您混淆了这些概念,QWidget的根不是QApplication,而是一个必要的需求。在您的情况下,您必须将QML应用程序嵌入到QWidget中,对于这一点,有几个替代方案,如本other answer中所示,但在本例中,我将使用QQuickView显示解决方案
对于上述内容,必须修改以下内容:
由于要使用QQuickView,因此不应使用ApplicationWindow,但根目录必须是一个项目,并重新分发工具栏、StackView和抽屉,以便维护GUI结构
由于我不使用.qrc,您必须将放置在“qrc:/pages/…”模型中的路径修改为“pages/…”
QQuickStyle类无法从PyQt访问,因此必须交替实现用于建立样式和图标的解决方案
考虑到上述情况,代码如下:
main.py
gallery.qml
可以找到完整的示例here
相关问题 更多 >
编程相关推荐