我是新开发的 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);
}
注册页面.hpp
class RegisterPage: public QObject
{
Q_OBJECT
public:
RegisterPage();
virtual ~RegisterPage() {}
Q_INVOKABLE void initRegistration();
};
注册页面.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!";
}
注册页面.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)
{
}
关于c++ - UI 在 ApplicationUI 之外不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34898516/