[英]My Blackberry 10 Cascades (C++, Qt & QML) sample app is crashing on startup with the following error
I am trying to make a sample Blackberry 10 Cascades app in C++, Qt, & QML in the QNX Momentics IDE, and run it in the Blackberry 10 Dev Alpha Simulator. 我正在尝试在QNX Momentics IDE中使用C ++,Qt和QML制作示例Blackberry 10 Cascades应用程序,并在Blackberry 10 Dev Alpha Simulator中运行它。 When I start running it, I get the following error messages:
当我开始运行它时,收到以下错误消息:
ApplicationPrivate::declarativeEngine: ERROR called from non-UI thread QThread(0x8067e48) ApplicationPrivate::declarativeEngine: Method called from non-UI thread ApplicationPrivate :: declarativeEngine:从非UI线程调用的错误QThread(0x8067e48)ApplicationPrivate :: declarativeEngine:从非UI线程调用的方法
I have tried googling those error messages - but I only find a few links that actually apply to my situation - and the solutions for those problems don't seem to apply to me. 我曾尝试使用Google搜索这些错误消息-但我只发现了一些实际适用于我的情况的链接-这些问题的解决方案似乎不适用于我。
I know it's got something to do with me trying to call different classes from different qml documents. 我知道尝试从不同的qml文档调用不同的类与我有关系。 (Which is something I was having trouble with before - which is why I made this sample app in the first place - to see if I could figure out how to call different classes from different qml documents).
(这是我以前遇到的麻烦-这就是为什么我首先制作此示例应用程序的原因-以查看是否可以弄清楚如何从不同的qml文档中调用不同的类)。
Here is the code of my sample app. 这是我的示例应用程序的代码。 There are 7 files (
main.qml
, main.cpp
, app.cpp
, app.hpp
, Page2.cpp
, Page2.h
, Page2.qml
). 有7个文件(
main.qml
, main.cpp
, app.cpp
, app.hpp
, Page2.cpp
, Page2.h
, Page2.qml
)。 I would much appreciate if anyone could help me solve this - because I am not really sure what I am doing wrong. 如果有人可以帮助我解决这个问题,我将不胜感激-因为我不确定自己做错了什么。
Thank you. 谢谢。
(I have taken out main.cpp
& reduced the code from the other 6 files to what I think are the key parts to this problem). (我已取出
main.cpp
并将代码从其他6个文件减少到我认为是解决此问题的关键部分)。
I know the problem has something to do with my Page2
class and the Page2.qml
file. 我知道问题与我的
Page2
类和Page2.qml
文件有关。
(I think the problem is that there is something I have to do to be able to call qt code from more than one qml files - and I just don't know what that is). (我认为问题是我必须做一些事情才能能够从多个qml文件中调用qt代码-我只是不知道那是什么)。
main.qml
: main.qml
:
import bb.cascades 1.0
//-- create one page with a label and text
Page {
content: Container {
Button {
text: "Page 2"
onClicked: app.goToPage2
}
}
}
from app.cpp
来自
app.cpp
App::App()
{
qmlRegisterType<App>("GDLiteBB", 1, 0, "app");
qmlRegisterType<Page2>("GDLiteBB", 1, 0, "Page2");
QmlDocument *qml = QmlDocument::create("main.qml");
qml->setContextProperty("app", this);
AbstractPane *root = qml->createRootNode<AbstractPane>();
Application::setScene(root);
}
void App::goToPage2() {
Page2::getInstance();
}
from app.hpp
来自
app.hpp
class App : public QObject
{
Q_OBJECT
public:
App();
Q_INVOKABLE void goToPage2();
};
Page2.cpp
QmlDocument * Page2::qml_ = QmlDocument::create("Page2.qml");
AbstractPane * Page2::root_ = qml_->createRootNode<AbstractPane>();
Label * Page2::label_ = root_->findChild<Label *>("label");
Page2::Page2() : QObject() {
++count_;
page2_ = this;
qml_->setContextProperty("Page2", this);
Application::setScene(root_);
}
Page2 *Page2::getInstance() {
return page2_ ? page2_ : new Page2();
}
void Page2::setLabel() {
label_->setText("This is page 2");
}
Page2.h
class Page2 : public QObject {
Q_OBJECT
static Page2 *page2_;
static unsigned int count_;
static bb::cascades::QmlDocument *qml_;
static bb::cascades::AbstractPane *root_;
static bb::cascades::Label *label_;
Page2();
public:
static Page2 *getInstance();
virtual ~Page2();
Q_INVOKABLE void setLabel();
};
Page2.qml
import bb.cascades 1.0
import GDLiteBB 1.0
Page {
content: Container {
Button {
text: "Set Label"
onClicked: Page2.setLabel()
}
Label {
objectName: "Label"
}
}
}
I think your problem is these lines: 我认为您的问题是以下几行:
QmlDocument * Page2::qml_ = QmlDocument::create("Page2.qml");
AbstractPane * Page2::root_ = qml_->createRootNode<AbstractPane>();
Label * Page2::label_ = root_->findChild<Label *>("label");
These objects are being created as soon as the application code loads into memory, which means that it's happening before the event loop starts in main()
. 一旦将应用程序代码加载到内存中,就会立即创建这些对象,这意味着它是在
main()
的事件循环开始之前发生的。 All classes that inherit from QObject need to be created after the event loop starts and destroyed before the event loop ends. 从QObject继承的所有类都需要在事件循环开始之后创建,并在事件循环结束之前销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.