# Using QVTKOpenGLWidget with vtkContextItem-derived class

**URL:** https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092
**Category:** Support
**Created:** [June 7, 2019, 12:03am UTC](https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092 "2019-06-07T00:03:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Kevin\_Hall](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kevin_hall/32/467_2.png) [@Kevin\_Hall](https://discourse.vtk.org/u/Kevin_Hall)
#### Post date: [June 7, 2019, 12:03am UTC](https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092/1 "2019-06-07T00:03:24Z")

</div>

Hi,

I’m really new to VTK and I’m in the process of both reading the VTK online book and also trying to get a simple Qt-based application using a vtkContextItem-derived class to draw to a vtkContext2D. Anyway, I’ve been able to get a window to show up, but the vtkContextItem-derived object’s Paint() method never gets called and nothing displays other than a black window. I’ve looked online for examples and there just aren’t may using QVTKOpenGLWidget and a vtkContextItem-derived class. I was curious if someone knew what I am missing to tie everything together properly. Does anyone know?

My test application’s code is below for reference.

Many, many thanks for the help!

Sincerely,

Kevin Hall

```
#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)

#include <QApplication>
#include <vtkNew.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkBrush.h>
#include <vtkContextActor.h>
#include <vtkContextItem.h>
#include <vtkContextScene.h>
#include <vtkObjectFactory.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkContext2D.h>
#include <QVTKOpenGLWidget.h>

class MyDrawing : public vtkContextItem
{
public:
    static MyDrawing *New();
    vtkTypeMacro(MyDrawing, vtkContextItem);
    virtual bool Paint(vtkContext2D* painter)
    {
        painter->GetBrush()->SetColor(255, 0, 0);
        painter->DrawRect(20,20,40,40);
        return true;
    }
};

vtkStandardNewMacro(MyDrawing);

int main(int argc, char** argv)
{
    QCoreApplication::addLibraryPath("../../../bin/Qt");

    QApplication app(argc, argv);

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

    vtkNew<vtkContextActor> cactor;
    vtkNew<MyDrawing> drawing;
    vtkNew<vtkRenderer> renderer;

    cactor->GetScene()->AddItem(drawing);
    renderer->AddActor( cactor );

    auto renderWindow = widget.GetRenderWindow();
    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);

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

    app.exec();

    return EXIT_SUCCESS;
}
```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [June 7, 2019, 2:35am UTC](https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092/2 "2019-06-07T02:35:00Z")

</div>

We use [ctkVTKChartView](https://github.com/commontk/CTK/blob/master/Libs/Visualization/VTK/Widgets/ctkVTKChartView.h) class (in CTK library) to show charts in our Qt-based application. You can find an example [here](https://github.com/commontk/CTK/blob/master/Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKChartViewTest1.cpp).

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [June 7, 2019, 7:48am UTC](https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092/3 "2019-06-07T07:48:30Z")

</div>

Hi Kevin,

I got your test case working (see below). I think what was mostly missing was to set up a QSurfaceFormat appropriate for QVTKOpenGLWidget.

**ctx.cpp**

```cpp
#include <QApplication>
#include <QSurfaceFormat>

#include <vtkNew.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkBrush.h>
#include <vtkContextActor.h>
#include <vtkContextItem.h>
#include <vtkContextScene.h>
#include <vtkObjectFactory.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkContext2D.h>
#include <QVTKOpenGLWidget.h>

class MyDrawing : public vtkContextItem
{
public:
    static MyDrawing *New();
    vtkTypeMacro(MyDrawing, vtkContextItem);
    virtual bool Paint(vtkContext2D* painter)
    {
        painter->GetBrush()->SetColor(255, 0, 0);
        painter->DrawRect(20,20,40,40);
        return true;
    }
};

vtkStandardNewMacro(MyDrawing);

int main(int argc, char** argv)
{
    QCoreApplication::addLibraryPath("../../../bin/Qt");

    QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());

    QApplication app(argc, argv);

    vtkNew<MyDrawing> drawing;

    vtkNew<vtkContextActor> cactor;
    cactor->GetScene()->AddItem(drawing);

    vtkNew<vtkRenderer> renderer;
    renderer->AddActor(cactor);

    vtkNew<vtkGenericOpenGLRenderWindow> window;
    window->AddRenderer(renderer);

    QVTKOpenGLWidget widget;
    widget.SetRenderWindow(window.Get());
    widget.resize( 256, 256 );
    widget.show();

    return app.exec();
}

```

**CMakeLists.txt**

```auto
cmake_minimum_required(VERSION 3.5)

project(ctx)

find_package(Qt5Widgets REQUIRED)

find_package(VTK 8.2.0 REQUIRED COMPONENTS
    vtkGUISupportQt
    vtkInteractionStyle
    vtkRenderingCore
    vtkRenderingContext2D
    vtkRenderingContextOpenGL2
    vtkRenderingFreeType
    vtkRenderingOpenGL2
)

add_executable(ctx WIN32
    ctx.cpp
)

target_link_libraries(ctx PRIVATE
    Qt5::Widgets
    vtkGUISupportQt
    vtkInteractionStyle
    vtkRenderingCore
    vtkRenderingContext2D
    vtkRenderingContextOpenGL2
    vtkRenderingFreeType
    vtkRenderingOpenGL2
)

target_include_directories(ctx SYSTEM PRIVATE
    ${VTK_INCLUDE_DIRS}
)

target_compile_definitions(ctx PRIVATE
    ${VTK_DEFINITIONS}
)

```

**Result**

![result](https://discourse.vtk.org/uploads/default/original/1X/b42740bf53ad824e26225ab87f4abf9b1df91a86.png)

Hope that helps.

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [June 7, 2019, 7:59am UTC](https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092/4 "2019-06-07T07:59:56Z")

</div>

Note also from the `QVTKOpenGLWidget` docs:

> Unlike [QVTKOpenGLNativeWidget](https://vtk.org/doc/nightly/html/classQVTKOpenGLNativeWidget.html), [QVTKOpenGLWidget](https://vtk.org/doc/nightly/html/classQVTKOpenGLWidget.html) does not require that the default surface format for the application be changed. One can simply specify the needed QSurfaceFormat for the specific [QVTKOpenGLWidget](https://vtk.org/doc/nightly/html/classQVTKOpenGLWidget.html) instance by calling `QVTKOpenGLWidget::setFormat` before the widget is initialized.

So

```
widget.setFormat(QVTKOpenGLWidget::defaultFormat());

```

will also work.

(Note the above is true for VTK 8.2.0 and higher. In versions prior, QVTKOpenGLNativeWidget was the only QOpenGLWidget-based widget, and it was called QVTKOpenGLWidget, and I think it required you to change the application default format)

---

<div class="post-metadata">

### Author: ![Kevin\_Hall](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kevin_hall/32/467_2.png) [@Kevin\_Hall](https://discourse.vtk.org/u/Kevin_Hall)
#### Post date: [June 7, 2019, 8:48am UTC](https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092/5 "2019-06-07T08:48:59Z")

</div>

Thank you Elvis. That was very helpful and got me unstuck! 🙂

---

<div class="post-metadata">

### Author: ![Kevin\_Hall](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kevin_hall/32/467_2.png) [@Kevin\_Hall](https://discourse.vtk.org/u/Kevin_Hall)
#### Post date: [June 7, 2019, 8:50am UTC](https://discourse.vtk.org/t/using-qvtkopenglwidget-with-vtkcontextitem-derived-class/1092/6 "2019-06-07T08:50:13Z")

</div>

Wow, I didn’t even know about the CTK library. There’s a lot of really cool things there. Once I get my feet under VTK, I’ll certainly take a look at that. Thank you!
