2012-02-29 12 views
21

से सी ++ फ़ंक्शन एक्सेस करें मैं क्यूटी के साथ एक छोटा प्रोग्राम बनाने की कोशिश कर रहा हूं। मैं निम्नलिखित कोड के साथ एक 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 

तो मैंने क्या गलत किया?

+0

कृपया निम्नलिखित धागा जाँच यह आपके http मदद कर सकता है: // stackoverflow.com/questions/5709820/communication-between-c-and-qml – shofee

+0

हाय, मैंने इसे चेक किया है, लेकिन यह सटीक रूप से नहीं है कि मैं क्या चाहता हूं ... मैं माउसएरा पर एक ऑनक्लिक ईवेंट जोड़ना चाहता हूं जो सीपीपी फ़ंक्शन को कॉल करता है। (जावास्क्रिप्ट में यह सिर्फ reken_tijden_uit() है, तो यह C++ और qml में कैसे काम करता है ... – Mathlight

उत्तर

33

क्यूएमएल से किसी भी सी ++ कोड को कॉल करने के लिए, इसे QObject के अंदर अवश्य अवश्य होना चाहिए।

आपको अपने काम के साथ QObject अवरोही कक्षा बनाने की आवश्यकता है, इसे QML पर पंजीकृत करें, इसे अपने क्यूएमएल में तुरंत चालू करें और फ़ंक्शन को कॉल करें। ध्यान दें कि आपको अपने फ़ंक्शन को Q_INVOKABLE के साथ चिह्नित करना होगा।

कोड:

#ifndef EIGEN_FUNCTION_HEADER_H 
#define EIGEN_FUNCTION_HEADER_H 

#include <QObject> 

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 <QtDeclarative> 

#include "qmlapplicationviewer.h" 
#include "eigen_function_header.h" 

Q_DECL_EXPORT int main(int argc, char *argv[]) 
{ 
    QScopedPointer<QApplication> app(createApplication(argc, argv)); 
    qmlRegisterType<MyObject>("com.myself", 1, 0, "MyObject"); 

    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: { 
      console.log(myobject.reken_tijden_uit()) 
     } 
    } 
} 
+0

और मुझे कोड का पहला भाग कहां रखना है ??? – Mathlight

+0

बेस्ट, एक अलग .cpp/.h जोड़ा गया अपनी परियोजना के लिए। qmlRegisterType – Koying

+0

ठीक करने से पहले ..c में मुख्य.cpp में शामिल करना न भूलें, मुझे कुछ त्रुटियां मिली हैं, मैं एक नया उत्तर पोस्ट करूंगा, इसलिए यह स्पष्ट है कि समस्या क्या है ... – Mathlight

0

एक विकल्प के रूप को qmlRegisterType() main.cpp में, आप क्यूओ बनाने के लिए संदर्भ गुणों का भी उपयोग कर सकते हैं QML में उपलब्ध बेजेबल चर। (यदि आपको बाद में क्यूएमएल के साथ अपने ऑब्जेक्ट के विभिन्न उदाहरण बनाने की आवश्यकता नहीं है)।

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(); 

    // add single instance of your object to the QML context as a property 
    // the object will be available in QML with name "myObject" 
    MyObject* myObject = new MyObject(); 
    viewer.engine()->rootContext()->setContextProperty("myObject", myObject); 

    return app->exec(); 
} 

QML में, आप तो वस्तु कहीं से भी अपने कोड में main.cpp में निर्दिष्ट दिए गए नाम के साथ उपयोग कर सकते हैं।कोई अतिरिक्त घोषणाओं की आवश्यकता:

MouseArea { 
    anchors.fill: parent 
    onClicked: { 
     myObject.reken_tijden_uit() 
    } 
} 

आप QML के बारे में अधिक जानकारी प्राप्त कर सकते < -> सी ++ संचार यहां संभावनाएं: https://v-play.net/cross-platform-development/how-to-expose-a-qt-cpp-class-with-signals-and-slots-to-qml

संबंधित मुद्दे