2012-12-11 1 views
8

그래서 몇 가지 검색을 수행했지만 읽은 유사한 질문에는 아무런 도움이되지 않았습니다.QML (Qt 빠른 응용 프로그램)에서 C++ 메서드 호출

저는 Qt Creator를 사용하고 있습니다. (그리고 저는 Qt에 너무 익숙하지 않습니다.) 그래서 백그라운드에서 어떤 부두교가 이루어 졌는지 모르겠습니다. 그러나 표준 Qt 빠른 응용 프로그램 프로젝트를 사용하고 있습니다.

본질적으로 레이아웃의 일부 텍스트를 주기적으로 대체하는 문자열을 반환하는 QML에서 C++ 함수를 호출하려고합니다. 여기

는 MAIN.CPP입니다 :

#include <QtGui/QApplication> 
#include "qmlapplicationviewer.h" 
#include <QDeclarativeContext> 


class testClass : public QObject 
{ 
    Q_OBJECT 
public: 
    Q_INVOKABLE QString gimmeText() { 
      return QString("new text"); 
} 
}; 



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/Picenter2/main.qml")); 

    testClass t; 
    viewer.rootContext()->setContextProperty("testOb", &t); 

    viewer.showFullScreen(); 

    return app->exec(); 
} 

그리고 여기 (그것의 대부분은 분명히 무관으로) 레이아웃의 미리보기입니다 :

Text { 
    id: text1 
    x: 105 
    y: 156 
    color: "#ffffff" 
    text: qsTr("text") 
    font.pixelSize: 12 
    Timer { 
     interval: 1000; running: true; repeat: false 
     onTriggered: text1.text = testOb.gimmeText(); 
    } 

주어진 오류는 다음과 같습니다

invalid use of incomplete type 'struct QDeclarativeContext' main.cpp (28) 
forward declaration of 'struct QDeclarativeContext' qdeclarativeview.h (60) 

EDIT : QDeclarativeContext를 포함하면 위의 내용이 사라지고 다음과 같은 오류가 발생합니다.

(.text.startup+0x3e):-1: error: undefined reference to `vtable for testClass' 
(.text.startup+0xcf):-1: error: undefined reference to `vtable for testClass' 
(.text.startup+0x12b):-1: error: undefined reference to `vtable for testClass' 
:-1: error: collect2: ld returned 1 exit status 

저는 C++ 프로그래밍을 많이 해본 적이 없으므로 그 의미가 무엇인지 잘 모릅니다. 본질적으로 동일한 문제에 대한 조언을하면 vtable 오류 또는 이해하기 어려운 것만 제공합니다.

정말 혼란 스럽습니다. 헤더 파일을 보면 QmlApplicationViewer가 QDeclarativeView에서 파생 된 것입니다. 바로 Qt 문서가 정확히 내가 원하는 것을 수행하기 위해 here을 사용하고 있습니다. 누구나 가지고있는 제안에 감사드립니다.

+0

오류 메시지가 컴파일러라는 클래스가 있다고 알고 있음을 의미한다 "QDeclarativeContext"당신의 코드는 다음과 같이한다 (예를 들어) 함수 호출을 해결하는 데 필요한 세부 사항을 알지 못합니다. 해당 클래스가 선언 된 머리글을 조회하고 해당 머리글을 예제에 포함하십시오. –

+0

그것이 제가 읽은 것입니다. 그래서, 나는 그것을 포함시켰다. 단지'vtable for testClass '오류에 대해 세 가지'정의되지 않은 참조 만 제공합니다. – Logan

+0

Qt 라이브러리와 올바르게 연결되어 있고 라이브러리가 올바른 순서로 제공되어 있습니까? –

답변

0

나는 qt에 대한 경험이 없으며 코드에서 오류를 유발하는 것을 볼 수 없습니다. 그러나 그러한 오류가 발생하면 클래스 (struct QDeclarativeContext)가으로 선언 된 이되었지만 전체 정의가 (액세스 멤버,이 유형의 변수 선언 등) 인 것처럼 사용되는 입니다. 이 문제를 해결하려면 에이 유형의 정의가있는 헤더가 있어야합니다.

+0

업데이트 된 질문. 이 오류를 수정할 수 있지만 더 발생합니다. – Logan

5

클래스를 QML과 함께 사용하려면 등록해야합니다. 주 기능에서이 작업을 수행 할 수 있습니다. QML 코드로 가져와야합니다.

MAIN.CPP :

#include <QtGui/QApplication> 
#include "qmlapplicationviewer.h" 
#include <QDeclarativeContext> 

#include <QtDeclarative> // Required for registration 


class testClass : public QObject 
{ 
    Q_OBJECT 
    public: 
    Q_INVOKABLE QString gimmeText() { 
      return QString("new text"); 
    } 
}; 



Q_DECL_EXPORT int main(int argc, char *argv[]) 
{ 
    QScopedPointer<QApplication> app(createApplication(argc, argv)); 

    qmlRegisterType<testClass>("MyCustomQMLObjects", 2, 35, "testClassNameInQML"); 

    QmlApplicationViewer viewer; 

    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); 
    viewer.setMainQmlFile(QLatin1String("qml/Picenter2/main.qml")); 

    testClass t; 
    viewer.rootContext()->setContextProperty("testOb", &t); 

    viewer.showFullScreen(); 

    return app->exec(); 
} 

QML 코드 :

// ... 

import MyCustomQMLObjects 2.35 

// ... 

property testClassNameInQML testOb 

// ... 

Text { 
    id: text1 
    x: 105 
    y: 156 
    color: "#ffffff" 
    text: qsTr("text") 
    font.pixelSize: 12 
    Timer { 
     interval: 1000; running: true; repeat: false 
     onTriggered: text1.text = testOb.gimmeText(); 
    } 

// ...