Different screen between qmlscene and Python : Toolbar

给你一囗甜甜゛ 提交于 2019-12-02 07:17:29

问题


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 want to know if it's normal. How can I do for have the same result with PyQt5

The result with qmlscene:

The result with Python:

Thanks you for your help.

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

File : _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
                }                   
            }
        }
    }
}


回答1:


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.



来源:https://stackoverflow.com/questions/57548057/different-screen-between-qmlscene-and-python-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!