Qt 创建QDeclarativeView对象会导致分段错误

Qt 创建QDeclarativeView对象会导致分段错误,qt,qt-quick,Qt,Qt Quick,.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:

.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;
  }
};
#包括
#包括
类MyClass:公共QObject
{
Q_对象
公众:
Q_可调用的void cppMethod(const QString&msg)
{

qDebug()请注意,您没有使用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();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QDEcrativeView视图;
view.show();
返回a.exec();
}

现在它运行并显示一个空视图,正如预期的那样…

这可能是一个错误。尝试使用较新的版本(5.1.1或4.8.5)。在调试器中运行它以查看崩溃的位置。然后添加
view.show()
#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();
}