我是新开发的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开发的基础,那就很抱歉,但这让我很疯狂。
----------------
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作为参数传递,我们可以在registerPage.cpp中找到qml元素。
谢谢!
答案 0 :(得分: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)
{
}