QWidget: Must construct a QApplication before a QWidget

Hi,

Getting the following error while try to run application.
QWidget: Must construct a QApplication before a QWidget

Below is a snippet of code.

#include <QApplication>

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>

#include <QVTKWidget.h>

int main(int argc, char** argv)
{
	QApplication app(argc, argv);

	QVTKWidget widget;
	widget.resize(256, 256);

	vtkSmartPointer<vtkSphereSource> sphereSource =
		vtkSmartPointer<vtkSphereSource>::New();

	vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	sphereMapper->SetInputConnection(sphereSource->GetOutputPort());

	vtkSmartPointer<vtkActor> sphereActor =
		vtkSmartPointer<vtkActor>::New();
	sphereActor->SetMapper(sphereMapper);

	vtkSmartPointer<vtkRenderer> renderer =
		vtkSmartPointer<vtkRenderer>::New();
	renderer->AddActor(sphereActor);

	widget.GetRenderWindow()->AddRenderer(renderer);
	widget.show();

	app.exec();

	return EXIT_SUCCESS;
}

VTK- 8.2.0
Qt- 5.12.2
VS- 2017

Can somebody please help me ?

Hi,

I’m stuck exactly with the same problem.

Did you find a solution? It’s been a while since you posted, but it would be much appreciated if your share the solution you found, if any.

Thanks in advance.
Best regards,
Alex

First hit on Google explains it: