linux - 将命令行参数传递给 qml
问题描述
我想从 linux shell 调用一个 qml-script 并传递一个文本作为参数,比如
./message.qml "hello this is a message"
或者
/usr/bin/qt5/qml ./message.qml "hello this is a message"
qml 脚本应该显示该文本。
下面的示例 qml 脚本有效,但显示的文本(“hello”)当然是静态的。是否可以在 qml 中查询命令行参数?
#!/usr/bin/qt5/qml
import QtQuick 2.2
Rectangle {
width: 1024
height: 600
Text {
anchors.centerIn: parent
text: "Hello" // here I want to have a text which is set in the call
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit()
}
}
}
解决方案
您可以使用 访问命令行参数Qt.application.arguments
,例如在我的情况下,如果我执行:
/usr/bin/qml message.qml "hello this is a message"
#!/usr/bin/qt5/qml
import QtQuick 2.2
Rectangle {
width: 1024
height: 600
Text {
anchors.centerIn: parent
text: Qt.application.arguments[2] // here I want to have a text which is set in the call
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit()
}
}
}
中的索引Qt.application.arguments[index]
可能会有所不同,具体取决于您调用 qml 执行的方式。
推荐阅读
- python - 比较日期和时间段
- python - 我的代码在不应该循环的时候不断循环
- python - 使用 sklearn 重新缩放多元时间序列数据
- mysql - 无法弄清楚如何编写简单的 SQL 语句
- r - ggplot geom_linerange 中的 Alpha 由 Mac 上的观察次数确定
- java - OKTA 在点击 /groups 端点时返回 400 错误
- windows - 试图将超过 9 个文件拖放到批处理脚本中
- android - 我想要没有 MAIN 片段或按钮的条件登录导航
- postgresql - PostgreSQL 的统计收集器是否跟踪索引的*所有*使用情况?
- android - Kotlin android开发diffutil itemCallback类型错误