。H
#include <QObject>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void cppMethod (const QString &msg)
{
qDebug() << "Called the C++ method with" << msg;
}
public slots:
void cppSlot (int number)
{
qDebug() << "Called the C++ slot with" << number;
}
};
.cpp
#include <QtCore/QCoreApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
#include "cppFromQml.h"
int main (int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDeclarativeView view;
return a.exec();
}
这导致分割错误。出路是什么?
Qt:4.8.1
请注意,您没有使用MyClass,而且-只是我的猜测-声明性视图将需要QApplication才能正确运行。
为了更好地理解,我创建了一个项目,几乎全部丢弃(只是保留了.pro,在其中添加了qt += declarative
),然后对您的代码进行了如下更改:
#include <QApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDeclarativeView view;
view.show();
return a.exec();
}
现在它运行并按预期显示空视图...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句