8

So, I've done some searching and none of the similar questions I've read have had advice that worked.

I'm using Qt Creator (and I'm not too familiar with Qt) so I'm not sure what voodoo it's done in the background. However, I'm using a standard Qt Quick Application project.

Essentially, I want to call a C++ function from QML that returns a string that replaces some text in the layout, periodically.

Here is 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();
}

And here is a snippet of the layout (as most of it is obviously irrelevant):

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

The errors given are:

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

EDIT: with QDeclarativeContext included, the above disappear, giving these errors:

(.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

I haven't done much C++ programming, so I'm not entirely familiar with what that means. Following advice for essentially identical problems has only given me vtable errors or more incomprehensible things.

What really confuses me is that, looking at the header file, the QmlApplicationViewer is derived from QDeclarativeView, which is exactly what the Qt documentation uses here to do almost exactly what I want. Thanks for any suggestions anyone has.

4

2 に答える 2

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

// ...
于 2012-12-16T02:27:12.220 に答える
0

私は経験がなくqt、コードの何がエラーを引き起こしているのかわかりません。ただし、このようなエラーが発生するのは、クラス (struct QDeclarativeContext) が前方宣言されているためですが、定義全体が既知であるかのように使用されます (メンバーへのアクセス、この型の変数の宣言など)。これを修正するには、このタイプの定義を持つヘッダーを含める必要があります。

于 2012-12-11T07:54:43.883 に答える