c++qtvisual-studio-2017qtscriptqt5.11

Issues using QScriptEngine: Must construct a Q(Core)Application before a QScriptEngine


I have a Qt Gui application using mainwindow widget. The window class looks like that:

class MainWindow : public QMainWindow
{
  Q_OBJECT
  ...
  public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
  //some more code
  private:
    class2* m_data;
}

In class2 I want to use qscriptengine. Briefly it looks like that:

class2.h:

 class class2: public QObject
 {
  Q_OBJECT
  public:
   class2(QObject* parent = nullptr);
 ...
 private:
 QScriptEngine* m_engine;
 }

class2.cpp:

 class2::class2(QObject* parent)
 :QObject(parent)       
{
 m_engine = new QScriptEngine(this); //this line fails!!
 //some other code
}

Also there is a main.cpp file, looking typically:

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 MainWindow w;
 w.show();
 return a.exec();
}

On initializing qscript engine application crushes. Debugger lead me to qscriptengine.cpp, error qFatal("QScriptEngine: Must construct a Q(Core)Application before a QScriptEngine");. It also shows unhandled exception in ucrtbase.dll. Surprisingly it works in release configuration (but fails somewhere), and fails on startup in debug configuration. Thought it could be relevant. What am I doing wrong and how can I fix it? I did have issues with including qtscript library to project, but it seems to me that I solved them. I added the lib to the project by #pragma comment and in project properties. Should I copy its .lib and .dll files or something like that?..


Solution

  • Problem is solved by reinstalling Qt and including libraries in project in additional dependencies, not by pragma comment.