我寫了一個模型與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並且問題消失了。無論如何,謝謝你的回覆。 –