Important: Please read the Qt Code of Conduct -

Very lost here, want to start a Qt Quick application, can't connect C++ code (5.2.1)

  • Bit of forewarning, I'm completely new to Qt.

    I've been following this great little tutorial:

    When I make my own Qt Quick Application project, I'm presented with two files, main.cpp & main.qml. I'm not concerned with learning how to use QML to do what I want just yet, at the moment I'm concerned with how to import a functional CPP file.

    I can create a new .cpp file right next to the .qml file, and name it "logic.cpp". Inside logic.cpp I have a function called hello().

    Now in my QML file, I can import it by writing
    @import "logic.cpp" as Logic@

    and then call the hello function inside my qml code

    @MouseArea {
    anchors.fill: parent
    onClicked: {

    No errors are being shown to me here until I try to compile. It tells me this:

    "logic.cpp": no such directory
    import "logic.cpp" as Logic

    Depending on where I place my .cpp file, it will transfer over to the build file or it wont, but I get the error either way.

    As you can see I'm lacking some basic fundamental knowledge on how to get a Qt Quick Application running properly, I would very much appreciate any pointers on how to get something functional working.


  • Code logic written in C++ is not imported that way.
    Look at "this": and in particular to "this":

    Basically you have the C++ code which calls the QML viewer. In that scope you can create instances of C++ classes and bind them to the QML scope. Once an object is bound it can be used just like you did in your wrong example.

    Enjoy! :)

  • Hi, I prefer to use "qmlRegisterType": to expose a c++ class to QML, so you can create objects from QML rather than c++.

Log in to reply