UI 应用程序外部没有响应UI

UI does not respond outside ApplicationUI

我刚开始开发 BlackBerry 应用程序,我真的很难理解它是如何工作的,因为 BB 的开发方式与 Android 或 iOS 开发方式有很大不同。

这是我的问题:我创建了一个 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 调用函数?为什么按钮没有反应?

非常感谢,如果这是黑莓开发的基础,我很抱歉,但这让我发疯了。

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

已编辑

最后感谢@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 作为参数传递,我们可以在 registerPage.cpp.

中找到 qml 元素

谢谢!

我不确定,但也许我的回答会对你有所帮助。

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)
{
}