当我在 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
}
}
}
}
}
最佳答案
分析source code qmlscene 并使用 --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_())
根据the docs 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.
看来图标的缩放是平台风格的一部分。
每种类型的项目都需要一个 QXApplication:
控制台应用程序:您可以使用 3 种类型的 QXApplication 中的任何一种,但使用 QCoreApplication 是最佳选择,因为其他 QXApplication 要求它们具有窗口系统,在这种情况下这是不必要的要求。
QML应用程序:至少需要一个QGuiApplication,但对于某些应用,例如需要使用各个平台的样式,则需要使用QApplication。
Qt Widgets 应用程序:QApplication 是必要的,因为 QWidgets 使用每个平台的样式。
尺寸发生变化,这是 QtQuick.Controls 1 的问题吗?
是的,QQC1 和 QQC2 之间的主要区别之一是,第一个是为支持桌面平台而开发的,因此您可以使用样式,而第二个是为嵌入式系统设计的,其主要目标是性能。欲了解更多信息,请阅读Differences with Qt Quick Controls 1
结论:
如果您希望使用 QML 制作的 GUI 尊重桌面平台的样式,那么您必须将 QQC1 与 QApplication 一起使用。
如果您的目标是应用程序的风格不尊重桌面的风格,并且希望获得更高的性能,那么您应该将 QQC2 与 QGuiApplication 一起使用。
关于python - qmlscene和Python之间的屏幕不同: Toolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57548057/