我做了一些搜索,但是所有我读到的类似问题都没有提供有效的建议。
我正在使用Qt Creator(并且我对Qt不太熟悉),因此我不确定它在后台做了什么。然而,我正在使用标准的Qt Quick Application项目。
本质上,我想从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)
编辑:包含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文档在此处使用的方法,几乎可以实现我想要的功能。感谢任何人提供的建议。
moc
应用程序生成带有元数据的头文件时,才能使用 make 命令使其正常工作。将 *.cpp 文件添加到 *.pro 文件中作为头文件可以产生相同的效果(但这是一种不好的方法)。 - Marek R