正如@Mark 所建议的,上下文属性可以在 QML 中用于在运行时决定是否启用某个宏,但是他提供的示例并未解决这种情况,当您需要根据此决定实例化对象时,但仅在代码块内覆盖大小写。所以我将提供我所做的完整示例。
在我的例子中,只有在编译时定义了宏时,我才向 QML 公开了一个类:
主.cpp :
#ifdef SMTP_SUPPORT
qmlRegisterType<SmtpClientHelper>("com.some.plugin", 1, 0, "SmtpClient");
engine.rootContext()->setContextProperty("SMTP_SUPPORT", QVariant(true));
#else
engine.rootContext()->setContextProperty("SMTP_SUPPORT", QVariant(false));
#endif // SMTP_SUPPORT
Run Code Online (Sandbox Code Playgroud)
然后在 QML 端我检查这个宏并决定是否必须创建 SmtpClient 对象:
qml :
import QtQuick 2.0;
import com.some.plugin 1.0
Item {
id: root
// ...
property var smtpClient // No inside any function, need to instantiate the object
Component.onCompleted: {
if (SMTP_SUPPORT) {
smtpClient = Qt.createQmlObject(' \
import QtQuick 2.0; \
import com.some.plugin 1.0; \
SmtpClient { \
id: smtpClient; \
\
function setSenderEmail(email) { \
senderEmail = email; \
storage.save("common", "clientEmail", email); \
} \
} \
', root, "SmtpClient");
}
}
// Reference smtpClient normally, like if it was statically created
TextInput {
id: senderEmailLogin
anchors.fill: parent
font.pixelSize: Globals.defaultFontSize
text: smtpClient ? smtpClient.senderEmail : ""
onEditingFinished: if (smtpClient) smtpClient.setSenderEmail(text)
activeFocusOnPress: true
}
}
Run Code Online (Sandbox Code Playgroud)
我认为 Loader 也应该完成这项工作,因此您可以创建一个单独的组件,如果定义了 C++ 宏,则在 Loadersource或sourceComponent属性中引用它,但我不确定,因为不确定是否会静态检查该组件(在我的情况下) )SmtpClient类型可用
它与#ifdef 不同,因为决策都是在运行时发生的,但我有时会使用这种方法,它提供了类似的使用模式。
主程序
QQmlApplicationEngine engine;
#ifdef DEMO_MODE
engine.rootContext()->setContextProperty("DEMO_MODE", QVariant(true));
#else
engine.rootContext()->setContextProperty("DEMO_MODE", QVariant(false));
#endif
engine.load(QUrl("qrc:/ui/MainQmlFile.qml"));
Run Code Online (Sandbox Code Playgroud)
然后,从项目中的任何 .qml 文件
if (DEMO_MODE) {
} else {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7970 次 |
| 最近记录: |