powerailin
powerailin

Reputation: 11

Qt with qwt runtime error: QWidget: Must construct a QApplication before a QWidget

I am trying to make a app using qt 5.4.1(and qwt 6.1.2).Here is my environment:

and I have built qwt with my Qt static libs successfully. I creat a widget class inherited from QwtPlot,and I creat a mainWindow which has a object of that widget. Then I build the project. However,there is a runtime error:QWidget: Must construct a QApplication before a QWidget.


This is my widget class inherited from QwtPlot:

#pragma once

#include <QWT/qwt_plot.h>
#include <QWT/qwt_plot_curve.h>



class DrawWidget: public QwtPlot
{
public:
	DrawWidget(QWidget *parent );
	~DrawWidget(void);
};

DrawWidget::DrawWidget(QWidget *parent )
	 :   QwtPlot( parent ),
	 carve(NULL)
{
	
}

And the follow is my mainWindow class:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include "DrawWidget.h"

#include <QtWidgets/QMainWindow>

class MainWindow : public QMainWindow
{
	Q_OBJECT


public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
 

private:
    DrawWidget *drawWidget;
	

};

#endif // MAINWINDOW_H

  MainWindow::MainWindow(QWidget *parent)
	  :QMainWindow(parent)
  {
     QWidget *widget = new QWidget(this);
     this->setCentralWidget(widget);
	 QHBoxLayout *mainLayout = new QHBoxLayout(widget);

	 drawWidget = new DrawWidget(widget);

     mainLayout->addWidget(drawWidget);
     centralWidget()->setLayout(mainLayout);

  }

And this is my main.cpp:

#include "mainwindow.h"
#include <QtWidgets/QApplication>
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);

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

I build this project in release version.Any idea? Thank you!

Upvotes: 1

Views: 3093

Answers (2)

lesolorzanov
lesolorzanov

Reputation: 3615

I found this very useful

http://www.itk.org/Wiki/VTK/FAQ#Shared_builds_of_VTK_and_debugging_QVTKWidget_using_Visual_Studio

It happens that libraries depend on the debug version of VTK so you have to build both release and debug and be sure to add the path in the debugging properties.

Upvotes: 0

Angel.Risteski
Angel.Risteski

Reputation: 566

I had the same problem few weeks ago in my case the problem was the additional library. Make sure that you got builds of the additional library debug for debug building and release for release building. Add AdditionalLibraryDebug/bin folder to the Path environment variable when you build Debug Version and AdditionalLibraryRelease/bin when you build Release version (not both in same time)

Good Luck :) i solved my problem on this way. :)

Upvotes: 2

Related Questions