Question

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

This results in segmentation fault. What's the way out?

Qt: 4.8.1

Was it helpful?

Solution

note that you're not using MyClass, and - just my guess - a declarative view will need a QApplication to properly run.

To better understand, I created a project, dumped almost all away (just kept the .pro, where I added qt += declarative), and changed a bit your code as follow:

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

now it runs and display an empty view, as expected...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top