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: 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 LogicDepending 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!
-
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! :)
-
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++.