2016-01-20 37 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没问题。我会很乐意提供帮助。现在试试我的解决方案也许现在它会工作。 –

相关问题