1

I try to register ColorsSource *pSource object to use it in QML but I get erreor in string: "QQmlContext *context = myObject->rootContext();" undefined reference to __imp__ZNK12QQuickWidget11rootContextEv

main.cpp

#include "ColorsSource.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQuickWidgets/QQuickWidget>
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    engine.load(url);

    ColorsSource *pSource = new ColorsSource;

    QQuickWidget *myObject = static_cast<QQuickWidget*>(engine.rootObjects().first());

    QQmlContext *context = myObject->rootContext();
    context->setContextProperty("ColorSource", pSource);

    return app.exec();
}
0

1 Answer 1

4

You are missing the quickwidgets Qt module. Thats why it cannot find these Symbols. If you use qmake, add QT += quickwidgets to your pro file.

But that won't be your Problem. QQuickWidgets are widgets to display some QQuick code, to be used in a Widgets application. But as you use a QQmlApplicationEngine, you are working in Quick. Your rootObject() won't be a QQuickWidget. So instead of looking for a QQuickWidget, access the engines rootContext - replace:

QQuickWidget *myObject = static_cast<QQuickWidget*>(engine.rootObjects().first());
QQmlContext *context = myObject->rootContext();
context->setContextProperty("ColorSource", pSource);

with

QQmlContext *context = engine.rootContext();
context->setContextProperty("ColorSource", pSource);

This will make your code a lot less prone to errors.

Also, in an unrelated note, you should always check your pointers, to make sure your static_cast went allright. Otherwise you will get crashes if the engines rootObjects are empty or the first one is not a QQuickWidget.

Sign up to request clarification or add additional context in comments.

4 Comments

I use CMake instead of qmake
@ЕвгенийДружинин - Similar procedure but again, you will not need the quickwidgets module if you are using the QQmlApplicationContext. Strip all the QQuickWidget includes and replace the stuff I wrote in my answer, this should fix things for you
Thanks it helped me, engine.rootContext() is really works
Good to hear. As a tip: Have a look into the differences between a QWidget based application and a QQuick based application. These are two entirely different concepts and understanding what you are working with makes a huge difference in your development.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.