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:

It does not help - same problem.

#include <QApplication>

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

#include <QVTKOpenGLNativeWidget.h>
QVTKOpenGLNativeWidget* widget;

void run()
{
    QVTKOpenGLNativeWidget* widget = new QVTKOpenGLNativeWidget();
    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();
}


int main(int argc, char** argv)
{
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    QApplication app(argc, argv);


    run();
    app.exec();

    return EXIT_SUCCESS;
}


I don’t see anything wrong in the code above. You can try to start from the tests that you can find in GUISupport/Qt/Testing/Cxx and there may be Qt examples in VTK Examples website, too.

I know it’s an old thread, but the solution is so unexpected, that every Google search hit should have the solution:
This problem also occurs, at least on Windows, if you run a Debug build of your application with a Release build of vtk. (I encountered this before, and now did so again on Vtk 9.3.0 and Qt 6.6.2.).
The solution for this – having Vtk-targets that support both Debug and Release libraries – is currently not trivial, since vtk does not support it direclty:

  1. Build and install vtk in both Debug and Release configuration. (Since about version 9.0.0 vtk has debug suffixes on .lib and .dll files, so they can be installed into the same folder.)
  2. In CMake, augment the result of your find_packag(Vtk...) call with the necessary debug *d.dll- and *d.lib-files, similar to this:
      find_package(VTK COMPONENTS CommonCore)

      get_target_property(Dll_CommonCore_Release VTK::CommonCore IMPORTED_LOCATION_RELEASE)
      get_target_property(Lib_CommonCore_Release VTK::CommonCore IMPORTED_IMPLIB_RELEASE)

      string(REPLACE ".dll" "d.dll" Dll_CommonCore_Debug ${Dll_CommonCore_Release})
      string(REPLACE ".lib" "d.lib" Lib_CommonCore_Debug ${Lib_CommonCore_Release})

      set_target_properties(VTK::CommonCore PROPERTIES
        IMPORTED_LOCATION_DEBUG  ${Dll_CommonCore_Debug}
        IMPORTED_IMPLIB_DEBUG    ${Lib_CommonCore_Debug}
      )