Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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");
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    And inside QML :

    ConnectionHandler{
            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':

    QGuiApplication

    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 project.pro 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