Nominate our 2022 Qt Champions!

Undefined reference to `QCheckBox::staticMetaObject'

  • Hi!

    I'm trying to access my QCheckBox from C++ (it's declared in my QML app) but always en up with this error because of this code :

    QCheckBox *obj = parent()->findChild<QCheckBox*>("cbThick");
    obj->setProperty("checked", (state!=0));

    The object trying to call it from has been declared inside QML too (it has a QObject tag).
    This is the way I declared the type in QML :

    int main(int argc, char *argv[])
        qmlRegisterType<ConnectionHandler>("MyHandler", 1, 0, "ConnectionHandler");
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();

    And inside QML :

            id: ch
            ip: tfIP.text
            port: tfPort.text

    I've seen the code I'm trying to compile on multiple places on forums but It seems the way I use it is different though...

    Any idea?

  • Lifetime Qt Champion

    @furest said in Undefined reference to `QCheckBox::staticMetaObject':


    QCheckBox is a QWidget so you will likely need to link against QtWidgets and use a QApplication.

  • Hi!
    Thank you a lot! I compiles (I think it must work then...)
    A bit of google research helped me (in case any newbie like me has trouble finding out):

    Link against widgets : edit your and add widget to that line, like this:

    QT += quick widgets

    Then just include QApplication in your main.cpp:

    #include <QApplication>

    And change your QGuiApplication app to a QApplication app

Log in to reply