[vtkusers] vtk and gtk on macOS

David Gobbi david.gobbi at gmail.com
Tue Oct 24 14:56:16 EDT 2017


Hi Steve,

Here is some code that I use to bind a VTK window to a QWidget.  My guess
is that something similar should work with GTK.  I have been using this on
OS X with vtkCocoaRenderWindow.

void BindRenderWindow(vtkRenderWindow *window, QWidget *widget)
{
  // Unmap the window if it is already mapped somewhere else.
  if (window->GetMapped()) {
    window->Finalize();
  }
  // Create the connection
  window->SetWindowId(reinterpret_cast<void *>(widget->winId()));
  // Note that we must call the superclass SetSize()/SetPosition()
  // (we just want to set the member variables, with no side-effects)
  window->vtkRenderWindow::SetSize(widget->width(), widget->height());
  window->vtkRenderWindow::SetPosition(widget->x(), widget->y());
  // Prepare for rendering
  if (widget->isVisible()) {
    window->Start();
  }
  // Call SetSize() again, to synchronize the window to the widget
  window->SetSize(widget->width(), widget->height());
}

Some further notes:
1) I disable Qt's paint engine, so that Qt itself doesn't draw in the
window.  GTK may be similar.
2) You probably will not need GtkGLExt (for Qt, I didn't have to use
QGLWidget).

 - David



On Tue, Oct 24, 2017 at 12:12 PM, Langer, Stephen A. (Fed) <
stephen.langer at nist.gov> wrote:

> Hi --
>
> Is there a simple way to get vtk to work inside a gtk+2 program on macOS?
>  If there isn't a simple way, is there a difficult one?
>
> I'm trying to get a program that uses gtk+2 and vtk to work with a modern
> version of vtk -- I'm upgrading from 5.10.1 to 7.1.1 or 8.0.1.    On Linux,
> the following code creates a vtk render window and a gtk widget containing
> it:
>     vtkRenderWindow *render_win = vtkRenderWindow::New();
>     GtkWidget *drawing_area = gtk_drawing_area_new();
>     Display *disp = GDK_DISPLAY();
>     render_win->SetDisplayId(disp);
> followed eventually by
>      XID wid = GDK_WINDOW_XID(drawing_area->window);
>      render_win->SetWindowId(wid);
> after receiving the gtk "realize" signal on the drawing_area.
>
> On macOS, I'd like to use the Cocoa version of gtk+2 and
> vtkCocoaRenderWindow, but I can't figure out how to embed the render window
> into a gtk widget.    All the examples I've found on-line aren't really
> applicable.
>
> GtkGLExt might be applicable, but it doesn't look like it's being
> maintained.
>
> An alternative would be to use X11, but native Mac OpenGL doesn't
> understand X11, so I tried installing mesa from macports , rebuilding vtk
> with VTK_USE_X instead of VTK_USE_COCOA, and linking to /opt/local/lib
> instead of /System/Library/Frameworks/OpenGL.framework.  Then I can use
> the same code as on Linux, but vtk complains about the context not
> supporting OpenGL 3.2, and then crashes:
>   ERROR: In /Users/langer/UTIL/VTK/VTK-7.1.1/Rendering/OpenGL2/vtkTextureObject.cxx,
> line 440
>   vtkTextureObject (0x7f9eafdb1690): failed at glGenTextures 1 OpenGL
> errors detected
>     0 : (1280) Invalid enum
>
> Thanks,
>      Steve
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171024/3df48674/attachment.html>


More information about the vtkusers mailing list