我写了一个模型与QML使用它,并适当地定义的所有属性:QML [未定义]错误
class Model: public QObject {
Q_OBJECT
Q_PROPERTY(QString title READ title CONSTANT)
Q_PROPERTY(QString request READ request CONSTANT)
Q_PROPERTY(int first READ first WRITE setFirst NOTIFY firstChanged)
Q_PROPERTY(int second READ second WRITE setSecond NOTIFY secondChanged)
Q_PROPERTY(int minimumValue READ minimumValue CONSTANT)
Q_PROPERTY(int maximumValue READ maximumValue CONSTANT)
Q_PROPERTY(bool isRange READ isRange CONSTANT)
Q_PROPERTY(int result READ result WRITE setResult)
... setters/getters/signals ...
}
一个然后我写了一个代码用于使用前一模型,该模型创建QML对话框的对象:
RangeInputDialog::Result RangeInputDialog::exec() const {
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(QUrl("qrc:///ui-common/ui/RangeInputDialog.qml"));
if (!component.isReady()) {
qDebug() << "Could not load range input dialog";
qDebug() << component.errors();
return Result::Error;
}
QQmlContext context(&engine);
context.setContextProperty("rangeDialogModel", &m_model);
QScopedPointer<QObject> window(component.create(&context));
if (window.isNull()) {
qDebug() << "Could not instance range input dialog";
return Result::Error;
}
QEventLoop loop;
QObject::connect(window.data(), SIGNAL(closing(QQuickCloseEvent *)), &loop, SLOT(quit()));
if (window->setProperty("visible", true)) {
loop.exec();
} else {
qDebug() << "Could not show range input dialog";
return Result::Error;
}
return static_cast<Result>(m_model.result());
}
setContextProperty
结合模型QML对话框和创建组件后它抛出一堆错误:
qrc:///ui-common/ui/RangeInputDialog.qml:23:9: Unable to assign [undefined] to QString
qrc:///ui-common/ui/RangeInputDialog.qml:39:10: Unable to assign [undefined] to QString
qrc:///ui-common/ui/RangeInputDialog.qml:58:19: Unable to assign [undefined] to double
qrc:///ui-common/ui/RangeInputDialog.qml:57:19: Unable to assign [undefined] to double
qrc:///ui-common/ui/RangeInputDialog.qml:56:12: Unable to assign [undefined] to double
qrc:///ui-common/ui/RangeInputDialog.qml:70:14: Unable to assign [undefined] to bool
qrc:///ui-common/ui/RangeInputDialog.qml:68:19: Unable to assign [undefined] to double
qrc:///ui-common/ui/RangeInputDialog.qml:67:12: Unable to assign [undefined] to double
有没有人知道这个问题。 我试图在清晰的解决方案中使用相同的代码,然后运行良好。
解决方案
好吧,我不知道为什么编译器没扔任何警告,但去除exec
方法const
符的解决了这个问题。
它没有改变。仅通过Model和QML连接的C++和QML仅用于GUI,它不能提供足够的性能。 –
@RealFresh编辑我的帖子,添加有关如何访问正确的上下文的信息。 – GrecKo
我只删除了const并且问题消失了。无论如何,谢谢你的回复。 –