Different screen between qmlscene and Python : Toolbar

前端 未结 1 1219
眼角桃花
眼角桃花 2021-01-23 02:40

I have a problem with Toolbar when I use the qml file with PyQt5. The result is not the seem : no background image when mouse is over, image no resize automatically.

I w

相关标签:
1条回答
  • 2021-01-23 03:06

    Analyzing the source code of qmlscene and testing with the --apptype option I get the following:

    qmlscene _test.qml --apptype gui
    

    qmlscene _test.qml --apptype widgets
    

    So analyzing the fundamental difference is that QApplicacion is being used and not QGuiApplication, so internally it should activate some flag that scales the icons.

    Considering the above, the solution is:

    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_())
    

    According to the docs of Qt Quick Controls 1:

    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.

    So it seems that the scaling of the icons is part of the style of the platform.


    Each type of project requires a QXApplication:

    • Console application: You can use any of the 3 types of QXApplication, but using QCoreApplication is the most optimal since the other QXApplication require that they have a window system that in that case is an unnecessary requirement.

    • QML Application: It requires at least one QGuiApplication, but for certain ones such as the need to use the styles of each platform it is necessary to use QApplication.

    • Qt Widgets Application: A QApplication is necessary because QWidgets use the styles of each platform.


    The fact that sizes change, is this a problem of QtQuick.Controls 1?

    Yes, one of the main differences between QQC1 and QQC2 is that the first one is developed to support desktop platforms so you use the styles, unlike the second one that is designed for embedded systems where the main objective is performance. For more information read Differences with Qt Quick Controls 1


    Conclusions:

    • If you want your GUI made with QML to respect the styles of your desktop platform then you must use QQC1 with QApplication.

    • If your goal is that the style of your application does not respect the style of the desktop in addition to wanting more performance you should use QQC2 with QGuiApplication.

    0 讨论(0)
提交回复
热议问题