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

时间:2013-10-01 11:22:48

标签: 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:
  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

1 个答案:

答案 0 :(得分:0)

请注意,您使用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();
}

现在它按预期运行并显示一个空视图......