Calling c++ function from qml
-
so i need to include main.moc in main.cpp file?
-
Yes, you will have to <code>#include "main.moc"</code> as well.
-
Lukas, when i include it, it is saying that
main.moc:no such file or directory
-
What kind of toolset are you using? qmake should add a call to <code>moc</code> automatically. You might take a look at your compile output if <code>moc</code> is acutally called.
If it is just Qt Creator which complains about the file missing this is absolutely correct. The file is generated at the time you compile your application and thus cannot be found earlier. Just compile your application.
If you are using Visual Studio you might need to add this step manually (a how to can be found in the forums).
-
Lukas, can u provide me a small example of how to call c++ functions from qml...this is what i am trying from morning and i am still unsuccessfull........and u asked me about toolset.....actually i am new to qml and qt....so i don't what it is exactly.....
-
Are you using Qt Creator? If so, if it is just Qt Creator, which complains about the file missing, just build and run your application. The file you are trying to include is generated on-the-fly when the application is built, and it is absolutely normal that it isn't available at development time.
!http://img208.imageshack.us/img208/766/filenotfounderror.png! This means you are fine.
Usually classes in general and classes subclassing QObject in particular are defined in <code>.h</code> files and implemented in <code>.cpp</code>. This way you do not need to mess around with including any generated files.
-
Lukas, i did as u told me....i included "main.moc"......i cleaned environment......i rebuilt the project and it is still prompting the same error on "build issues" panel ........
-
Well, you then either
- do it the way it is supposed to be done or
[quote author="Lukas Geyer" date="1327401808"]Usually classes in general and classes subclassing QObject in particular are defined in <code>.h</code> files and implemented in <code>.cpp</code>. This way you do not need to mess around with including any generated files.[/quote] - you pack and upload your current application and link it here, so I can take a look on it.
- do it the way it is supposed to be done or
-
http://www.mediafire.com/?b9a9r9t6qq9aoao
i uploaded the project.....have a look at it.....thanks in advance......
-
<code>main.moc</code> is generated as expected and the project compiles fine using MinGW and MSVC2010. Be aware that you have to <code>#include "main.moc"</code> at the end of the source file (or at least after the class definition).
I don't know what's the problem on your end.
Is there any reason you are not just doing it as it is supposed to be done?
[quote author="Lukas Geyer" date="1327401808"]Usually classes in general and classes subclassing QObject in particular are defined in <code>.h</code> files and implemented in <code>.cpp</code>. This way you do not need to mess around with including any generated files.[/quote]
-
i am using qt creator......
actually till yesterday i was doing programs only using qml.....this is my first project using both cpp and qml ......
i thought it was easy but it's not.......
-
It actually is easy.
Please don't get me wrong - but I have told you for the third time now how it is supposed to be done and how to circumvent the need of including generated files and I still sense that you do not even properly read what I write.
@
// testclass.hclass TestClass : public QObject
{
Q_OBJECTpublic:
TestClass(QObject *parent = 0);Q_INVOKABLE QString returnString();
};
// testclass.cpp
TestClass::TestClass(QObject *parent) : QObject(parent)
{
}QString TestClass::returnString()
{
return "string";
}// main.cpp
int main(int argc, char *argv[])
{
QApplication application(argc, argv);QDeclarativeView view; view.rootContext()->setContextProperty("TestClass", new TestClass(&view)); view.setSource(QUrl::fromLocalFile("Test.qml")); view.show(); return application.exec();
}
// Test.qml
import QtQuick 1.1
Text {
text: TestClass.returnString()
}
@ -
thanks a ton Lukas.....it worked....
-
Hey guys,
I´m having exactly the same problem, but I think some of the Classes and Libraries have changed. The way Lukas explained this makes a lot of sense to me, but the code doesn´t work for me. I´m using Qt 5.3 with QtQuick 2.2 .
I would be very thankful if someone could look at my code, as I´m desperate at the moment.My goal is to write a String into an xml-file.
the c++function should be called from qml.Thanks in advance
@
//header.h
class TestClass : public QObject
{
Q_OBJECTpublic:
TestClass(QObject *parent = 0);Q_INVOKABLE void writeXml(QString &text){ }
};@
TestClass::TestClass(QObject *parent) : QObject(parent)
{
}void TestClass::writeXml(QString text)
{QString filename = "C:/Qt5/Tools/QtCreator/bin/CPPTEST2/example.xml"; QFile file(filename); if (file.open(QIODevice::ReadWrite)) { QTextStream stream(&file); stream << text << endl; } file.close();
}@
@
//main.cpp
{
QGuiApplication app(argc, argv);QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); QQuickView *view = new QQuickView; view->setSource(QUrl::fromLocalFile("qrc:///main.qml")); view->show(); return app.exec();
}
@
@ //main.qml
import QtQuick 2.2
import QtQuick.Window 2.1Window {
visible: true
width: 360
height: 360MouseArea { anchors.fill: parent onClicked: { TestClass.writeXml("Hello World"); } }
}@
-
Hi mathiii
this is not how it works.
You have to create a C++ object first.
Than make the object for QML visible.
Now you can access the object.Check out the following link.
It describes every C++/QML interaction:
http://qt-project.org/wiki/Introduction_to_Qt_Quick_for_Cpp_developers#e70ea8482b12de725ec779f1bc55c92cBest regards