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. Very lost here, want to start a Qt Quick application, can't connect C++ code (5.2.1)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.2k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    nomad5
    wrote on last edited by
    #1

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

    I've been following this great little tutorial: http://qt-project.org/doc/qt-4.7/declarative-tutorials-samegame-samegame1.html

    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: {
    Logic.hello();
    }
    }@

    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.

    Thanks!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      deleted529
      wrote on last edited by
      #2

      Code logic written in C++ is not imported that way.
      Look at "this":http://qt-project.org/doc/qt-5/qtqml-cppintegration-topic.html and in particular to "this":http://qt-project.org/doc/qt-5/qtqml-cppintegration-contextproperties.html.

      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! :)

      1 Reply Last reply
      0
      • X Offline
        X Offline
        Xander84
        wrote on last edited by
        #3

        Hi, I prefer to use "qmlRegisterType":http://qt-project.org/doc/qt-5/qqmlengine.html#qmlRegisterType to expose a c++ class to QML, so you can create objects from QML rather than c++.

        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