से सी ++ फ़ंक्शन एक्सेस करें मैं क्यूटी के साथ एक छोटा प्रोग्राम बनाने की कोशिश कर रहा हूं। मैं निम्नलिखित कोड के साथ एक main.cpp
है:क्यूएमएल
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
viewer.showExpanded();
return app->exec();
}
int reken_tijden_uit(){
return true;
}
और मैं एक .qml
फ़ाइल है:
import QtQuick 1.1
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
अब, जब मैं MouseArea
पर क्लिक करें, कार्यक्रम इस्तीफा। मुझे क्या चाहिए कि यह reken_tijden_uit
को main.cpp
फ़ाइल में फ़ंक्शन को कॉल करता है।
मैंने बहुत कुछ किया है, और इस साइट पर खोज की है। मुझे कुछ जवाब मिल गए हैं, लेकिन मुझे एक काम नहीं मिला।
तो मैं कौन सा कोड डालूं जहां मैं C++ में reken_tijden_uit
फ़ंक्शन को कॉल कर सकता हूं?
अग्रिम धन्यवाद।
हेडर फाइल इस तरह दिखता है:
#ifndef EIGEN_FUNCTION_HEADER_H
#define EIGEN_FUNCTION_HEADER_H
class MyObject : public QObject{
Q_OBJECT
public:
explicit MyObject (QObject* parent = 0) : QObject(parent) {}
Q_INVOKABLE int reken_tijden_uit(){
return 1;
}
};
#endif // EIGEN_FUNCTION_HEADER_H
main.cpp
:
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "eigen_function_header.h"
QScopedPointer<QApplication> app(createApplication(argc, argv));
qmlRegisterType<MyObject>("com.myself", 1, 0, "MyObject");
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
viewer.showExpanded();
return app->exec();
}
और QML फ़ाइल:
import QtQuick 1.1
import com.myself 1.0
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MyObject {
id: myobject
}
MouseArea {
anchors.fill: parent
onClicked: {
myobject.reken_tijden_uit()
}
}
}
और त्रुटियों निम्न हैं:
D:\*\main.cpp:6: error: 'argc' was not declared in this scope
D:\*\main.cpp:6: error: 'argv' was not declared in this scope
D:\*\main.cpp:8: error: expected constructor, destructor, or type conversion before '<' token
तो मैंने क्या गलत किया?
कृपया निम्नलिखित धागा जाँच यह आपके http मदद कर सकता है: // stackoverflow.com/questions/5709820/communication-between-c-and-qml – shofee
हाय, मैंने इसे चेक किया है, लेकिन यह सटीक रूप से नहीं है कि मैं क्या चाहता हूं ... मैं माउसएरा पर एक ऑनक्लिक ईवेंट जोड़ना चाहता हूं जो सीपीपी फ़ंक्शन को कॉल करता है। (जावास्क्रिप्ट में यह सिर्फ reken_tijden_uit() है, तो यह C++ और qml में कैसे काम करता है ... – Mathlight