2016-01-20 98 views
2

我是新開發的黑莓應用程序,我很難理解它是如何工作的,因爲開發BB的方式與很多Android或iOS開發不同。用戶界面不響應外部應用程序UI

這是我的問題:我創建了一個registerPage.qml和一個registerPageController(與它的.hpp和.cpp)。我希望應用程序以registerPage.qml開頭,並調用表單qml中的一些方法,我在.cpp文件中寫入。

如果我在ApplicationUI()之外創建qml文檔,頁面顯示很好,但按鈕不響應。

我想用代碼是很容易理解的:

applicationui.cpp

ApplicationUI::ApplicationUI() : QObject() 
{ 
    m_pTranslator = new QTranslator(this); 
    m_pLocaleHandler = new LocaleHandler(this); 

    bool res = QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged())); 
    Q_ASSERT(res); 
    Q_UNUSED(res); 

    onSystemLanguageChanged(); 

    // --> OPTION 1 (what I want) <-- 
    /* If I init the register page here, the page is 
     displayed ok, but buttons does not respond. 
     The RegisterPage initialization code is below. */ 
    // RegisterPage registerPage; 

    // --> OPTION 2 <-- 
    /* If I create the registerPage here, buttons respond 
     but the _register param is not setted properly*/ 
     QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this); 
     qml->setContextProperty("_register", this); 

     AbstractPane *root = qml->createRootObject<AbstractPane>(); 
     Application::instance()->setScene(root); 
} 

registerPage.hpp

class RegisterPage: public QObject 
{ 
    Q_OBJECT 
public: 
    RegisterPage(); 
    virtual ~RegisterPage() {} 

    Q_INVOKABLE void initRegistration(); 
}; 

registerPage.cpp

RegisterPage::RegisterPage() 
{ 
    /* With this initialization, buttons does not respond!!*/ 
    QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this); 
    qml->setContextProperty("_register", this); 

    // Create root object for the UI 
    AbstractPane *root = qml->createRootObject<AbstractPane>(); 


    // Set created root object as the application scene 
    Application::instance()->setScene(root); 
} 

void RegisterPage::initRegistration() 
{ 
    qDebug() << "start registration!"; 
} 

registerPage。 qml

Page { 
    Container { 
     Button { 
      text: "Accept" 
      horizontalAlignment: HorizontalAlignment.Fill 
      topMargin: 100.0 
      appearance: ControlAppearance.Primary 
      onClicked: { 
       console.log("click!!") 
       _register.initRegistration() 
      } 
     } 
    } 
} 

我該如何加載一個qml文件,將它與.cpp相關聯,並從qml中調用函數?爲什麼按鈕不響應?

非常感謝,對不起,如果這是黑莓開發的基礎,但這讓我發瘋。

----------------

EDITED

最後感謝@Filip Hazubski幫我找到最終的解決方案。

applicationui.cpp

QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this); 
AbstractPane *root = qml->createRootObject<AbstractPane>(); 

RegisterPage* registerPage = new RegisterPage(this, root); 
qml->setContextProperty("_register", registerPage); 

Application::instance()->setScene(root); 

registerPage.cpp

RegisterPage::RegisterPage(QObject *parent, AbstractPane *root) : QObject(parent) 
{ 
    phoneTextField = root->findChild<TextField*>("phoneTextField"); 
} 

薪火AbstractPane爲PARAM我們可以發現QML元素融入registerPage.cpp。

謝謝!

回答

2

我不確定,但也許我的回答會幫助你。

applicationui.cpp代替:

QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this); 
qml->setContextProperty("_register", this); 

AbstractPane *root = qml->createRootObject<AbstractPane>(); 
Application::instance()->setScene(root); 

嘗試:

QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this); 
AbstractPane *root = qml->createRootObject<AbstractPane>(); 

RegisterPage* registerPage = new RegisterPage(this); 
qml->setContextProperty("_register", registerPage); 

Application::instance()->setScene(root); 

registerPage。HPP

RegisterPage(); 

變化

RegisterPage(QObject *parent = 0); 

registerPage.cpp變化

RegisterPage::RegisterPage() 
{ 
    /* With this initialization, buttons does not respond!!*/ 
    QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this); 
    qml->setContextProperty("_register", this); 

    // Create root object for the UI 
    AbstractPane *root = qml->createRootObject<AbstractPane>(); 


    // Set created root object as the application scene 
    Application::instance()->setScene(root); 
} 

RegisterPage::RegisterPage(QObject *parent) : QObject(parent) 
{ 
} 
+0

是的,我已經試過了也行,不過w ^如果我嘗試從程序崩潰的qml文件中調用RegisterPage的方法。感謝您的答覆! – kemmitorz

+0

@kemmitorz我會盡力改善它。 –

+0

@kemmitorz沒問題。我會很樂意提供幫助。現在試試我的解決方案也許現在它會工作。 –