本文整理汇总了C++中QQmlEngine::rootContext方法的典型用法代码示例。如果您正苦于以下问题:C++ QQmlEngine::rootContext方法的具体用法?C++ QQmlEngine::rootContext怎么用?C++ QQmlEngine::rootContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QQmlEngine
的用法示例。
在下文中一共展示了QQmlEngine::rootContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: contextForObject
void tst_qqmlengine::contextForObject()
{
QQmlEngine *engine = new QQmlEngine;
// Test null-object
QVERIFY(!QQmlEngine::contextForObject(0));
// Test an object with no context
QObject object;
QVERIFY(!QQmlEngine::contextForObject(&object));
// Test setting null-object
QQmlEngine::setContextForObject(0, engine->rootContext());
// Test setting null-context
QQmlEngine::setContextForObject(&object, 0);
// Test setting context
QQmlEngine::setContextForObject(&object, engine->rootContext());
QCOMPARE(QQmlEngine::contextForObject(&object), engine->rootContext());
QQmlContext context(engine->rootContext());
// Try changing context
QTest::ignoreMessage(QtWarningMsg, "QQmlEngine::setContextForObject(): Object already has a QQmlContext");
QQmlEngine::setContextForObject(&object, &context);
QCOMPARE(QQmlEngine::contextForObject(&object), engine->rootContext());
// Delete context
delete engine; engine = 0;
QVERIFY(!QQmlEngine::contextForObject(&object));
}
示例2: rootContext
void tst_qqmlengine::rootContext()
{
QQmlEngine engine;
QVERIFY(engine.rootContext());
QCOMPARE(engine.rootContext()->engine(), &engine);
QVERIFY(!engine.rootContext()->parentContext());
}
示例3: main
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
// Use QCommandLineParser to parse arguments (see documentation)
QQmlEngine engine;
// Register needed types
registerQmlTypes();
// Add image provider
engine.addImageProvider(QStringLiteral("svgelement"), new qtouch::SvgElementProvider(QQmlImageProviderBase::Image,
QUrl(QStringLiteral("qrc:///images/"))));
qtouch::DataModel dataModel;
try
{
dataModel.init();
}
catch (qtouch::Exception& e)
{
qCritical() << e.message();
return EXIT_FAILURE;
}
qtouch::CourseModel* courseModel = new qtouch::CourseModel(&dataModel, &app);
qtouch::ProfileModel* profileModel = new qtouch::ProfileModel(&dataModel, &app);
// Embed view models
engine.rootContext()->setContextProperty("$courseModel", courseModel);
engine.rootContext()->setContextProperty("$profileModel", profileModel);
// Create root component
QQmlComponent component(&engine);
QQuickWindow::setDefaultAlphaBuffer(true);
component.loadUrl(QUrl(QStringLiteral("qrc:/qml/MainWindow.qml")));
if (componentError(&component))
return EXIT_FAILURE;
if (component.isReady())
{
component.create();
if (componentError(&component))
return EXIT_FAILURE;
}
else
{
qWarning() << component.errorString();
return EXIT_FAILURE;
}
// FIXME: Not nice but fixes initialization problem
courseModel->selectCourse(0);
return app.exec();
}
示例4: syntaxError
// QTBUG-21310 - crash test
void tst_qqmlexpression::syntaxError()
{
QQmlEngine engine;
QQmlExpression expression(engine.rootContext(), 0, "asd asd");
QVariant v = expression.evaluate();
QCOMPARE(v, QVariant());
}
示例5: controlledWrite
void tst_QQmlPropertyMap::controlledWrite()
{
MyPropertyMap map;
QCOMPARE(map.isEmpty(), true);
//make changes in QML
QQmlEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty(QLatin1String("testdata"), &map);
const char *qmlSource =
"import QtQuick 2.0\n"
"Item { Component.onCompleted: { testdata.key1 = 'Hello World'; testdata.key2 = 'Goodbye' } }";
QQmlComponent component(&engine);
component.setData(qmlSource, QUrl::fromLocalFile(""));
QVERIFY(component.isReady());
QObject *obj = component.create();
QVERIFY(obj);
delete obj;
QCOMPARE(map.value(QLatin1String("key1")), QVariant("HELLO WORLD"));
QCOMPARE(map.value(QLatin1String("key2")), QVariant("Goodbye"));
}
示例6: changed
void tst_QQmlPropertyMap::changed()
{
QQmlPropertyMap map;
QSignalSpy spy(&map, SIGNAL(valueChanged(const QString&, const QVariant&)));
map.insert(QLatin1String("key1"),100);
map.insert(QLatin1String("key2"),200);
QCOMPARE(spy.count(), 0);
map.clear(QLatin1String("key1"));
QCOMPARE(spy.count(), 0);
//make changes in QML
QQmlEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty(QLatin1String("testdata"), &map);
QQmlComponent component(&engine);
component.setData("import QtQuick 2.0\nText { text: { testdata.key1 = 'Hello World'; 'X' } }",
QUrl::fromLocalFile(""));
QVERIFY(component.isReady());
QQuickText *txt = qobject_cast<QQuickText*>(component.create());
QVERIFY(txt);
QCOMPARE(txt->text(), QString('X'));
QCOMPARE(spy.count(), 1);
QList<QVariant> arguments = spy.takeFirst();
QCOMPARE(arguments.count(), 2);
QCOMPARE(arguments.at(0).toString(),QLatin1String("key1"));
QCOMPARE(arguments.at(1).value<QVariant>(),QVariant("Hello World"));
QCOMPARE(map.value(QLatin1String("key1")), QVariant("Hello World"));
}
示例7: main
int main(int argc, char **argv)
{
QApplication app(argc, argv);
const QUrl documentUrl = QUrl("qrc:///main.qml");
QQmlEngine engine;
#ifdef Q_OS_MACOS
engine.addImportPath(QStringLiteral("%1/../PlugIns").arg(QCoreApplication::applicationDirPath()));
#else
engine.addImportPath(PLUGIN_IMPORT_PATH);
#endif
Editor editor;
engine.rootContext()->setContextProperty("_editor", &editor);
QObject::connect(&engine, &QQmlEngine::quit, QCoreApplication::instance(), &QCoreApplication::quit);
QQmlComponent component(&engine, documentUrl);
QWidget *widget = qobject_cast<QWidget*>(component.create());
if (!widget)
qFatal("Failed to create widget from QML");
widget->show();
return app.exec();
}
示例8: engineSetContextForObject
void engineSetContextForObject(QQmlEngine_ *engine, QObject_ *object)
{
QQmlEngine *qengine = reinterpret_cast<QQmlEngine *>(engine);
QObject *qobject = reinterpret_cast<QObject *>(object);
QQmlEngine::setContextForObject(qobject, qengine->rootContext());
}
示例9: TEST_FILE
void TestSimpleQmlLoad::compileAndLoadSignal2()
{
const QString TEST_FILE(":/testqml/testsignal2.qml");
QQmlEngine *engine = new QQmlEngine;
QQmlComponent* component = compileAndLoad(engine, TEST_FILE);
QVERIFY(component);
SignalTester st;
engine->rootContext()->setContextProperty("st", &st);
QObject *myObject = component->create();
QVERIFY(myObject != NULL);
QVariant var = myObject->property("sigReceived");
QVERIFY(!var.isNull());
QVERIFY(var.toBool() == false);
st.sendSig();
var = myObject->property("sigReceived");
QVERIFY(!var.isNull());
QVERIFY(var.toBool() == true);
delete component;
delete engine;
}
示例10: main
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlEngine engine;
QMLBattleStationContext* context = new QMLBattleStationContext();
engine.rootContext()->setContextProperty(QString("context"), context);
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
component.create();
// Init/create subsystems
if (SDL_Init(SDL_INIT_JOYSTICK)) {
QString* err = new QString(SDL_GetError());
qWarning() << LOG_ERROR << "SDL init error:" << err;
// TODO UI notification
} else {
qDebug() << LOG_OK << "SDL init";
}
Serial* serial = new Serial();
InputHandler* inputHandler = new InputHandler();
// Connect Serial and BSC
QObject::connect(context, SIGNAL(SelectedSerialDeviceChanged(QString)),
serial, SLOT(SetActiveSerialDevice(QString)));
// Connect Input and BSC
QObject::connect(context, SIGNAL(SelectedJoystickAChanged(QString)),
inputHandler, SLOT(SetMainJoystick(QString*)));
// Connect Input to Serial
QObject::connect(inputHandler, SIGNAL(MotorValuesChanged(quint8[])),
serial, SLOT(SetMotorValues(quint8[])));
QObject::connect(inputHandler, SIGNAL(PostToolUpdate(quint16,quint16)),
serial, SLOT(EnqueueToolEvent(quint16,quint16)));
return app.exec();
}
示例11: init
void Editor::init()
{
if(m_initialized){
return;
}
Engine* engine = Engine::getInstance();
QQmlEngine* qmlEngine = engine->getQmlEngine();
QQmlContext* context = qmlEngine->rootContext();
context->setContextProperty("editor",this);
editorUiComponent = new QQmlComponent(qmlEngine,Util::getUrlPathToAsset("qml/editor/EditorUi.qml"));
for(QQmlError error :editorUiComponent->errors()){
qDebug() << "Error on Line" << error.line() << ":" << error.description();
}
if(editorUiComponent->isReady()){
initUi();
}else {
connect(editorUiComponent,SIGNAL(statusChanged(QQmlComponent::Status)),
this,SLOT(onLoadingUiChanged(QQmlComponent::Status)));
}
m_initialized = true;
}
示例12: addPages
void WelcomeMode::addPages(const QList<IWelcomePage *> &pages)
{
QList<IWelcomePage *> addedPages = pages;
Utils::sort(addedPages, [](const IWelcomePage *l, const IWelcomePage *r) {
return l->priority() < r->priority();
});
// insert into m_pluginList, keeping m_pluginList sorted by priority
QQmlEngine *engine = m_welcomePage->engine();
auto addIt = addedPages.begin();
auto currentIt = m_pluginList.begin();
while (addIt != addedPages.end()) {
IWelcomePage *page = *addIt;
QTC_ASSERT(!m_idPageMap.contains(page->id()), ++addIt; continue);
while (currentIt != m_pluginList.end() && (*currentIt)->priority() <= page->priority())
++currentIt;
// currentIt is now either end() or a page with higher value
currentIt = m_pluginList.insert(currentIt, page);
m_idPageMap.insert(page->id(), page);
page->facilitateQml(engine);
++currentIt;
++addIt;
}
// update model through reset
QQmlContext *ctx = engine->rootContext();
ctx->setContextProperty(QLatin1String("pagesModel"), QVariant::fromValue(
Utils::transform(m_pluginList, // transform into QList<QObject *>
[](IWelcomePage *page) -> QObject * {
return page;
})));
}
示例13: exception
void tst_qqmlexpression::exception()
{
QQmlEngine engine;
QQmlExpression expression(engine.rootContext(), 0, "abc=123");
QVariant v = expression.evaluate();
QCOMPARE(v, QVariant());
QVERIFY(expression.hasError());
}
示例14: newPacket
void MainView::newPacket() {
QQmlEngine *engine = new QQmlEngine;
QQmlComponent component(engine);
engine->rootContext()->setContextProperty("__packet__", new EthernetDisplay());
component.loadUrl(QUrl(QStringLiteral("qrc:/views/ethernet.qml")));
if (component.isReady())
component.create();
}
示例15: QQmlComponent
QQmlComponent_ *newComponent(QQmlEngine_ *engine, QObject_ *parent)
{
QQmlEngine *qengine = reinterpret_cast<QQmlEngine *>(engine);
//QObject *qparent = reinterpret_cast<QObject *>(parent);
QQmlComponent *qcomponent = new QQmlComponent(qengine);
// Qt 5.2.0 returns NULL on qmlEngine(qcomponent) without this.
QQmlEngine::setContextForObject(qcomponent, qengine->rootContext());
return qcomponent;
}