2

After lots of trouble I finally managed to set up VTK with QT using Visual Studio. The setup was done using cmake.

The versions I use are:

  • Visual Studio 2013
  • Qt 5.6.2
  • VTK 8.0.0

My first goal was to be able to run examples like this.

I'm able to run that examples and others in Release mode in VS and correctly run the executables.

The problem is when I set it to Debug Mode I will end up with an Error:

The program '[9520] SideBySideRenderWindowsQt.exe' has exited with code 1 (0x1).

After a few tries I found out that in Debug mode, whenever a QVTKWidget is created:

QVTKWidget *qvtkWidgetLeft;
(...)
qvtkWidgetLeft = new QVTKWidget(centralwidget);

I get the above error.

I've set up a debugger in QTcreator following this post How to configure CDB in Qt Creator?

EDIT:

The callstack if it helps:

>   SideBySideRenderWindowsQt.exe!QFlags<enum Qt::WindowType>::QFlags<enum Qt::WindowType>(int * __formal) Line 112 C++
    SideBySideRenderWindowsQt.exe!Ui_SideBySideRenderWindowsQt::setupUi(QMainWindow * SideBySideRenderWindowsQt) Line 72    C++
    SideBySideRenderWindowsQt.exe!SideBySideRenderWindowsQt::SideBySideRenderWindowsQt() Line 19    C++
    SideBySideRenderWindowsQt.exe!main(int argc, char * * argv) Line 9  C++

EDIT 2: Example

This example is from generating a new project of type QT GUI Application in Visual Studio 2013

See the comments in ui_QtGuiApplication.h

main.cpp

#include "QtGuiApplication2.h"
#include <QtWidgets/QApplication>

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

QtGuiApplication2.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication2.h"

class QtGuiApplication2 : public QMainWindow
{
    Q_OBJECT

public:
    QtGuiApplication2(QWidget *parent = Q_NULLPTR);

private:
    Ui::QtGuiApplication2Class ui;
};

QtGuiApplication2.cpp

#include "QtGuiApplication2.h"

QtGuiApplication2::QtGuiApplication2(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

ui_QtGuiApplication.h

This file is generated after using QT Design via Visual Studio:

#ifndef UI_QTGUIAPPLICATION2_H
#define UI_QTGUIAPPLICATION2_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_QtGuiApplication2Class
{
public:
    QWidget *centralWidget;
    QVTKWidget *qvtkWidget;   //Generated after adding VTK Widget
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *QtGuiApplication2Class)
    {
        if (QtGuiApplication2Class->objectName().isEmpty())
            QtGuiApplication2Class->setObjectName(QStringLiteral("QtGuiApplication2Class"));
        QtGuiApplication2Class->resize(600, 400);
        centralWidget = new QWidget(QtGuiApplication2Class);
        centralWidget->setObjectName(QStringLiteral("centralWidget"));

        //THE LINES BELOW ARE GENERATED IF I 
        //ADD A VTK WIDGET FROM QT DESIGN:
        //In Debug mode, the first line returns:
        //The program '[15392] QtGuiApplication2.exe' has exited with code 1 (0x1).
        //In Release mode, the program works fine.
        qvtkWidget = new QVTKWidget(centralWidget);
        qvtkWidget->setObjectName(QStringLiteral("qvtkWidget"));
        qvtkWidget->setGeometry(QRect(200, 100, 100, 100));


        QtGuiApplication2Class->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(QtGuiApplication2Class);
        menuBar->setObjectName(QStringLiteral("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 600, 21));
        QtGuiApplication2Class->setMenuBar(menuBar);
        mainToolBar = new QToolBar(QtGuiApplication2Class);
        mainToolBar->setObjectName(QStringLiteral("mainToolBar"));
        QtGuiApplication2Class->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(QtGuiApplication2Class);
        statusBar->setObjectName(QStringLiteral("statusBar"));
        QtGuiApplication2Class->setStatusBar(statusBar);

        retranslateUi(QtGuiApplication2Class);

        QMetaObject::connectSlotsByName(QtGuiApplication2Class);
    } // setupUi

    void retranslateUi(QMainWindow *QtGuiApplication2Class)
    {
        QtGuiApplication2Class->setWindowTitle(QApplication::translate("QtGuiApplication2Class", "QtGuiApplication2", 0));
    } // retranslateUi

};

namespace Ui {
    class QtGuiApplication2Class: public Ui_QtGuiApplication2Class {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_QTGUIAPPLICATION2_H
iehrlich
  • 3,572
  • 4
  • 34
  • 43
remi
  • 937
  • 3
  • 18
  • 45
  • Why you use QtCreator for debugging if you're setting the project in Visual Studio? Use the visual studio debugger. When it crashes check the callstack. It seems something like a invalid pointer somewhere but from your post I can't confirm it. – Jepessen Jun 27 '17 at 14:36
  • I don't want to debug with QT creator. After searching for a solution I thought I had to setup a debugger in qtcreator to correctly debug from VS. I might be wrong. But anyway I run the default VS debugger. I will check what the callstack says. – remi Jun 27 '17 at 14:39
  • Qt Creator setup is indeed irrelevant if you use Visual Studio. Posting the stack may be helpful. – m7913d Jun 27 '17 at 14:42
  • Thank you. Done. – remi Jun 27 '17 at 14:43
  • The program does not crash in the constructor of `QVTKWidget`. What is written in `setupUi` on line 72? – m7913d Jun 27 '17 at 14:56
  • @m7913d qvtkWidgetLeft = new QVTKWidget(centralwidget); is line 72. – remi Jun 27 '17 at 21:10
  • Stepping into it, it jumps directly to: `Q_DECL_CONSTEXPR inline QFlags(Zero = Q_NULLPTR) Q_DECL_NOTHROW : i(0) {}` in `qFlag` class, followed by returning to line 72.. (I guess this comment got a bit messy, but let me know if I can give you some info...) – remi Jun 27 '17 at 21:16
  • Can you construct a [mcve]? – m7913d Jun 27 '17 at 21:34
  • Yes I should do that. But first time I use VTK and QT, and everything is a bit confusing. Since this works fine when building in Release but not in Debug, I was hoping someone has encountered a similar problem. – remi Jun 27 '17 at 21:38
  • @m7913d I've added an example – remi Jun 28 '17 at 06:55
  • Is it a minimal example? Will `void main(int argc, char *argv[]) {QApplication a(argc, argv); QVTKWidget *pWidget = new QVTKWidget(); a.exec();}` also result in a crash? Note that a minimal example will help us to localise the problem. – m7913d Jun 28 '17 at 08:20
  • I'm currently writing an answer to the question. Found at last a solution. But yes I believe it should run and crash if QT and VTK are correctly setup. – remi Jun 28 '17 at 08:23

1 Answers1

1

Answer to my own problem

As I already had built VTK in both Debug and Release mode I thought that was in order.

But I saw in the output that my solution was getting the dll files from ...\VTK\VTK-bin\bin\Release\ which was added in Environment variables as a PATH.

I solved this by also adding ...\VTK\VTK-bin\bin\Debug\.

In environment variables under the PATH i know have:

C:\VTK\VTK-bin\bin\Debug
C:\VTK\VTK-bin\bin\Release

As Debug is listed first, it will get the dlls from this folder now, and I can correctly run in debug mode. (Atm the moment I need to switch the order of these two variables if I want to build in Release mode, so would still have to find a better solution for how the project finds the dlls.)

An additional problem that happened for me was a new error, it is described here: no override found for 'vtkPolyDataMapper' , and as stated in the solution it was resolved by adding:

#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)

in the beginning of QVTKwidget.h

remi
  • 937
  • 3
  • 18
  • 45