qmlscene 和 Python 之间的不同屏幕:工具栏

Different screen between qmlscene and Python : Toolbar

当我在 PyQt5 中使用 qml 文件时,工具栏出现问题。结果不是这样的:鼠标悬停时没有背景图像,图像没有自动调整大小。

我想知道这是否正常。 我怎样才能得到与 PyQt5

相同的结果

qmlscene 的结果:

Python的结果:

感谢您的帮助。

文件:_test.py


from PyQt5.QtCore import (
    pyqtProperty,
    pyqtSignal,
    pyqtSlot,
    QAbstractListModel,
    QModelIndex,
    QObject,
    Qt,
    QTimer,

)
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtQuick import QQuickView


class MainWindow(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)



if __name__ == "__main__":
    import sys

    app = QGuiApplication(sys.argv) 
    engine = QQmlApplicationEngine()
    engine.quit.connect(app.quit)
    main_window = MainWindow()
    engine.load("_test.qml")
    if not engine.rootObjects():
        sys.exit(app.exec_())

    sys.exit(app.exec())

文件:_test.qml


import QtQuick 2.4
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.3
import QtQuick.Controls.Styles 1.3

ApplicationWindow {
    width: 500
    height: 200
    visible: true

    ToolBar {
        Layout.fillWidth: true

        RowLayout {
            anchors.fill: parent


            ToolButton {
                //width: parent.height
                anchors.margins: 4                  
                iconSource: "ico/abacus.png"
            }

            ToolButton {
                width: parent.height
                Image {
                    source: "ico/quitter.png"
                    anchors.fill: parent
                    anchors.margins: 4
                }                   
            }

            ToolButton {
                width: parent.height
                iconSource: "ico/start.png"
                anchors.margins: 4
            }

            ToolButton {
                width: parent.height
                Image {
                    source: "ico/stop.png"
                    anchors.fill: parent
                    anchors.margins: 4
                }                   
            }
        }
    }
}

分析 qmlscene 的 source code 并使用 --apptype 选项进行测试我得到以下信息:

qmlscene _test.qml --apptype gui

qmlscene _test.qml --apptype widgets

所以分析根本区别是使用的是QApplicacion而不是QGuiApplication,所以在内部它应该激活一些缩放图标的标志。

综合以上,解决方案是:

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine


if __name__ == "__main__":
    import os
    import sys

    app = QApplication(sys.argv)

    engine = QQmlApplicationEngine()
    current_dir = os.path.dirname(os.path.realpath(__file__))
    file = os.path.join(current_dir, "_test.qml")
    engine.load(QUrl.fromLocalFile(file))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

根据 Qt Quick Controls 1 的 the docs

Note: We are using QApplication and not QGuiApplication in this example. Though you can use QGuiApplication instead, doing this will eliminate platform-dependent styling. This is because it is relying on the widget module to provide the native look and feel.

看来图标的缩放是平台风格的一部分。


每种类型的项目都需要一个 QXApplication:

  • 控制台应用程序:您可以使用 3 种类型的 QXApplication 中的任何一种,但使用 QCoreApplication 是最佳选择,因为其他 QXApplication 要求它们具有 window 系统,在这种情况下是不必要的要求。

  • QML Application: 至少需要一个QGuiApplication,但是有些需要使用各个平台的样式就需要QApplication了。

  • Qt Widgets Application:QApplication是必须的,因为QWidgets使用了各个平台的样式。


尺寸变化的事实,这是QtQuick.Controls1的问题吗?

是的,QQC1 和 QQC2 之间的主要区别之一是第一个是为支持桌面平台而开发的,因此您可以使用样式,而第二个是为嵌入式系统设计的主要 objective 是性能。有关详细信息,请阅读 Differences with Qt Quick Controls 1


结论:

  • 如果您希望使用 QML 制作的 GUI 符合桌面平台的样式,那么您必须将 QQC1 与 QApplication 结合使用。

  • 如果您的目标是您的应用程序的风格除了想要更高的性能之外还不尊重桌面的风格,您应该将 QQC2 与 QGuiApplication 一起使用。