【发布时间】:2020-01-13 15:37:55
【问题描述】:
您如何在 Qt 5.5 的 QML 文件中调用例如 QFile::exists(path)?
MyFile.qml
import QtQuick 2.5
// These are some various things I've tried, with the error message they
// give related to the row where I call QFile::exists()
#include <QFile> // Expected token `{'
import QtQml 2.5 // Expected token `;'
import io.qt // Expected token `;'
import io.qt.QFile // Expected token `;'
Item {
id: root
property string imageSource
Image {
id: test
source: fileOrFallback(root.imageSource)
}
function fileOrFallback (source) {
return QFile::exists(source)
? preprocessor.getFilePath(source)
: theme.example + 'placeholder.png'
}
}
我已经看到了一些关于如何导入自定义 Qt 函数的示例,但是如何在 QML 中调用内置 Qt 函数?
【问题讨论】: