Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qml call c++ function with CMake
Qt 6.11 is out! See what's new in the release blog

Qml call c++ function with CMake

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 806 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohannesSchaffer
    wrote on last edited by JohannesSchaffer
    #1

    If i use qmake, the following way works fine: If i want call a function from the class Test2, i do the following:
    test2.h:

    #ifndef TEST2_H
    #define TEST2_H
    
    #include <QObject>
    
    class Test2: public QObject
    {
        Q_OBJECT
    public:
        Test2();
    public slots:
        int testit();
    };
    
    #endif // TEST2_H
    

    test2.cpp:

    Test2::Test2()
    {
    
    }
    
    int Test2::testit()
    {
        return 6;
    }
    

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "test2.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        QQmlContext* context= engine.rootContext();
        Test2 test2instance;
        context->setContextProperty("test", &test2instance);
    
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    

    CMakeList:

    cmake_minimum_required(VERSION 3.1)
    
    project(QMLCmake LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt5 COMPONENTS Core Quick REQUIRED)
    
    add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")
    target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
    target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
    
    

    So i can call test.testit() in qml. But if i do this with CMake it doesn't work. The error message is: error: undefined reference to `Test2::Test2()'.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      When you don't add test2.cpp to the sources to compile the linker also cant find the class...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JohannesSchaffer
        wrote on last edited by
        #3

        If i have a normal c++ project, i didn't add the source file, too.
        But if i add

        set( project_sources
           main.cpp
           test2.cpp
        )
        

        it doesn't work, either.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JohannesSchaffer
          wrote on last edited by
          #4

          Thank you very much. I don't added the source file to executable. Now all works fine.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved