1

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?..

GFd
  • 21
  • 5
  • When did `class2` construct? Did you make it be the global variable? – JustWe Oct 02 '18 at 01:00
  • perhaps I should have added a piece of mainwindow.cpp code. Class2 is its part and constructs in mainwindow constructor: mainwindow.cpp: MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); m_data = new class2; } – GFd Oct 02 '18 at 09:31
  • I'm unable to reproduce this. Where do you instantiate `class2`? What issues with including scripts did you have? Do you have `QT += script` in your *.pro file? – Jaa-c Oct 03 '18 at 17:14
  • I don/t have a .pro file, I need a debugger so my project is in visual studio. Only sln file. Issues were not including script by plain "include < QScript/qscriptengine.h>, I had to use #pragma comment(lib, "QScript"). Class2 instance appears in mainwindow's constructor. Also important: it crushes only in debug configuration. In release it is fine. – GFd Oct 04 '18 at 12:20
  • Problem is solved by reinstalling Qt and including libraries in project in additional dependencies, not by pragma comment. – GFd Oct 07 '18 at 19:23

1 Answers1

1

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

GFd
  • 21
  • 5