Qt로 약간의 프로그램을 만들려고합니다. 나는 다음과 같은 코드를 사용하여 main.cpp
있습니다QML의 C++ 함수에 액세스
#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
를 클릭하면 프로그램이 종료합니다. 내가 원하는 것은 main.cpp
파일에서 함수 reken_tijden_uit
을 호출한다는 것입니다.
저는 많이 봤는데,이 사이트에서 검색했습니다. 나는 몇 가지 대답을 찾았지만 한 가지를 얻지는 못했습니다.
그래서 어떤 코드를 어디에 넣어야합니까? 그래서 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
안녕하세요, 내가 그것을 확인하지만, 정확히 내가 원하는 건 아니에요 ... 나는 cpp 함수를 호출하는 mousearea에 onclick 이벤트를 추가하고 싶습니다. (자바 스크립트에서 그냥 reken_tijden_uit(), 어떻게 C++ 및 qml 작동합니까 ... – Mathlight