问题描述
当我在 PyQt5 中使用 qml 文件时,工具栏出现问题.结果不是看起来:鼠标悬停时没有背景图像,图像没有自动调整大小.
我想知道这是否正常.我该怎么做才能得到与 PyQt5 相同的结果
qmlscene 的结果:
Python 的结果:
感谢您的帮助.
文件:_test.py
从 PyQt5.QtCore 导入(pyqt属性,pyqt信号,pyqt插槽,QAbstractListModel,QModelIndex,QObject,Qt,定时器,)从 PyQt5.QtGui 导入 QGuiApplication从 PyQt5.QtQml 导入 QQmlApplicationEngine从 PyQt5.QtQuick 导入 QQuickView类 MainWindow(QObject):def __init__(self, parent=None):super().__init__(parent)如果 __name__ == "__main__":导入系统app = QGuiApplication(sys.argv)引擎 = QQmlApplicationEngine()engine.quit.connect(app.quit)main_window = MainWindow()engine.load("_test.qml")如果不是 engine.rootObjects():sys.exit(app.exec_())sys.exit(app.exec())
文件:_test.qml
导入 QtQuick 2.4导入 QtQuick.Layouts 1.1导入 QtQuick.Controls 1.3导入 QtQuick.Controls.Styles 1.3应用程序窗口{宽度:500高度:200可见:真实工具栏 {Layout.fillWidth: 真行布局{anchors.fill:父级工具按钮 {//宽度:父级.高度锚点.边距:4iconSource: "ico/abacus.png"}工具按钮 {宽度:parent.height图像 {来源:ico/quitter.png"anchors.fill:父级锚点.边距:4}}工具按钮 {宽度:parent.heighticonSource: "ico/start.png"锚点.边距:4}工具按钮 {宽度:parent.height图像 {来源:ico/stop.png"anchors.fill:父级锚点.边距:4}}}}}分析
qmlscene _test.qml --apptype 小部件
所以分析根本区别是使用的是 QApplicacion 而不是 QGuiApplication,所以它应该在内部激活一些缩放图标的标志.
综合以上,解决办法是:
from PyQt5.QtCore import QUrl从 PyQt5.QtWidgets 导入 QApplication从 PyQt5.QtQml 导入 QQmlApplicationEngine如果 __name__ == "__main__":导入操作系统导入系统app = QApplication(sys.argv)引擎 = QQmlApplicationEngine()current_dir = os.path.dirname(os.path.realpath(__file__))file = os.path.join(current_dir, "_test.qml")engine.load(QUrl.fromLocalFile(file))如果不是 engine.rootObjects():sys.exit(-1)sys.exit(app.exec_())
根据文档:
注意:我们在这里使用的是 QApplication 而不是 QGuiApplication例子.虽然您可以使用 QGuiApplication 代替,但这样做会消除依赖于平台的样式.这是因为它依赖提供原生外观和感觉的小部件模块.
所以看起来图标的缩放是平台风格的一部分.
每种类型的项目都需要一个 QXApplication:
控制台应用程序:您可以使用 3 种 QXApplication 中的任何一种,但使用 QCoreApplication 是最佳选择,因为其他 QXApplication 要求它们具有窗口系统,而在这种情况下,这是不必要的要求.
QML Application:它至少需要一个QGuiApplication,但是对于某些需要使用各个平台的样式的需要使用QApplication.
Qt Widgets 应用程序:一个 QApplication 是必要的,因为 QWidgets 使用每个平台的样式.
尺寸变了,这是QtQuick.Controls 1的问题吗?
是的,QQC1 和 QQC2 之间的主要区别之一是,第一个是为支持桌面平台而开发的,因此您可以使用这些样式,而第二个是为主要目标是性能的嵌入式系统设计的.如需更多信息,请阅读与 Qt Quick Controls 1 的区别
结论:
如果您希望使用 QML 制作的 GUI 尊重桌面平台的样式,那么您必须将 QQC1 与 QApplication 结合使用.
如果您的目标是您的应用程序风格除了想要更高的性能之外不尊重桌面风格,您应该将 QQC2 与 QGuiApplication 结合使用.
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
}
}
}
}
}
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:
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.
这篇关于qmlscene 和 Python 之间的不同屏幕:工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!