我是新开发的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调用函数?为什么按钮不响应?
非常感谢,如果这是BlackBerry开发的基础,则感到抱歉,但这让我发疯。
----------------
编辑过
最后,感谢@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)
{
}