2016-05-12 29 views
0

我寫了一個模型與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符的解決了這個問題。

回答

2

當您加載RangeInputDialog.qml文件時,它會嘗試訪問rangeDialogModel上下文屬性,但它尚不存在,導致這些未定義的警告。

此外,當您在做QQmlContext context(&engine);時,您正在創建新的上下文,而不是檢索用於創建組件的上下文。 您應該通過調用engine.getRootContext()然後在該上下文中調用setContextProperty來獲取它,以便您的組件可以訪問上下文屬性。

您應該在component.loadUrl()之前執行context.setContextProperty()


作爲備註:QML中不是所有的用戶界面?從C++顯示QML對話框對我來說似乎很奇怪。 C++業務層不應該知道QML UI層。

+0

它沒有改變。僅通過Model和QML連接的C++和QML僅用於GUI,它不能提供足夠的性能。 –

+0

@RealFresh編輯我的帖子,添加有關如何訪問正確的上下文的信息。 – GrecKo

+0

我只刪除了const並且問題消失了。無論如何,謝謝你的回覆。 –