[vtkusers] QVTKWidget issue and workaround for OSX, retina display and Qt5

Simon ESNEAULT simon.esneault at gmail.com
Mon Feb 2 10:24:41 EST 2015


Hello,

There is a bug in QVTKWidget on OSX with a retina display, when build with
Qt5. Any QVTKWidget will display only the bottom left quarter, like this :

Here is a very simple program that reproduce the bug :

----------------------------------------------------------------------------------------------
*#include <QApplication>*
*#include <vtkActor.h>*
*#include <vtkPolyDataMapper.h>*
*#include <vtkSmartPointer.h>*
*#include <vtkSphereSource.h>*
*#include <vtkRenderer.h>*
*#include <vtkRenderWindow.h>*
*#include <QVTKWidget.h>*

*#define VTK_NEW(type, instance) \*
*  vtkSmartPointer<type> instance = vtkSmartPointer<type>::New();*

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

*  VTK_NEW(vtkSphereSource, sphereSource);*
*  VTK_NEW(vtkPolyDataMapper, sphereMapper);*
*  VTK_NEW(vtkActor, sphereActor);*
*  VTK_NEW(vtkRenderWindow, renderWindow);*
*  VTK_NEW(vtkRenderer, renderer);*

*  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());*
*  sphereActor->SetMapper(sphereMapper);*
*  renderWindow->AddRenderer(renderer);*
*  renderer->SetBackground(0.1, 0.3, 0.4);*
*  renderer->AddActor(sphereActor);*
*  widget.SetRenderWindow(renderWindow);*
*  widget.resize(256,256);*
*  widget.show();*

*  app.exec();*

*  return 0;*
*}*
----------------------------------------------------------------------------------------------

and the CMakeLists.txt :
----------------------------------------------------------------------------------------------
*cmake_minimum_required(VERSION 3.1)*
*if(POLICY CMP0053)*
*  cmake_policy(SET CMP0053 NEW)*
*endif()*
*project(QtImageViewer)*

*find_package(VTK COMPONENTS vtkGUISupportQt )*
*include(${VTK_USE_FILE})*
*find_package(Qt5Core REQUIRED QUIET)*

*add_executable(qtimageviewer **main.cxx**)*
*qt5_use_modules(qtimageviewer Core Gui Widgets)*
*target_link_libraries(qtimageviewer ${VTK_LIBRARIES})*
----------------------------------------------------------------------------------------------

To enable retina on a normal display, use this :
http://cocoamanifest.net/articles/2013/01/turn-on-hidpi-retina-mode-on-an-ordinary-mac.html
----------------------------------------------------------------------------------------------

Now to get back to a normal behavior, like it was with Qt4, one need to
call :
*setWantsBestResolutionOpenGLSurface:NO *on the* NSView *of the QVTKWidget !

Here is an example on how to solve the problem on the small example above :

main.cxx
----------------------------------------------------------------------------------------------
*#include <QApplication>*
*#include <vtkActor.h>*
*#include <vtkPolyDataMapper.h>*
*#include <vtkSmartPointer.h>*
*#include <vtkSphereSource.h>*
*#include <vtkRenderer.h>*
*#include <vtkRenderWindow.h>*
*#include <QVTKWidget.h>*

*#define VTK_NEW(type, instance) \*
*  vtkSmartPointer<type> instance = vtkSmartPointer<type>::New();*

*#ifdef Q_OS_OSX*
*#include "osxHelper.h"*
*#endif*

*int main(int argc, char** argv)*
*{*
*  QApplication app(argc, argv);*
*  QVTKWidget widget;*
*#ifdef Q_OS_OSX*
*  disableGLHiDPI(widget.winId());*
*#endif*

*  VTK_NEW(vtkSphereSource, sphereSource);*
*  VTK_NEW(vtkPolyDataMapper, sphereMapper);*
*  VTK_NEW(vtkActor, sphereActor);*
*  VTK_NEW(vtkRenderWindow, renderWindow);*
*  VTK_NEW(vtkRenderer, renderer);*

*  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());*
*  sphereActor->SetMapper(sphereMapper);*
*  renderWindow->AddRenderer(renderer);*
*  renderer->SetBackground(0.1, 0.3, 0.4);*
*  renderer->AddActor(sphereActor);*
*  widget.SetRenderWindow(renderWindow);*
*  widget.resize(256,256);*
*  widget.show();*

*  app.exec();*

*  return 0;*
*}*

----------------------------------------------------------------------------------------------

osxHelper.h
----------------------------------------------------------------------------------------------
*#ifndef _OSX_HELPER_*
*#define _OSX_HELPER_*
*void disableGLHiDPI( long a_id );*
*#endif*
----------------------------------------------------------------------------------------------

osxHelper.mm
----------------------------------------------------------------------------------------------
*#include <Cocoa/Cocoa.h>*
*#include "osxHelper.h"*

*void disableGLHiDPI( long a_id ){*
* NSView* view = reinterpret_cast<NSView*>( a_id );*
* [view setWantsBestResolutionOpenGLSurface:NO];*
*}*
----------------------------------------------------------------------------------------------

CMakeLists.txt
----------------------------------------------------------------------------------------------
*cmake_minimum_required(VERSION 3.1)*
*if(POLICY CMP0053)*
*  cmake_policy(SET CMP0053 NEW)*
*endif()*
*project(QtImageViewer)*

*find_package(VTK COMPONENTS vtkGUISupportQt )*
*include(${VTK_USE_FILE})*
*find_package(Qt5Core REQUIRED QUIET)*
*find_library( LIB_COCOA cocoa )*

*add_executable(qtimageviewer **main.cxx osxHelper.mm**)*
*qt5_use_modules(qtimageviewer Core Gui Widgets)*
*target_link_libraries(qtimageviewer ${VTK_LIBRARIES})*
----------------------------------------------------------------------------------------------
And there you go, problem solved ...

I hope this is useful to others, maybe this can be integrated in the
upcoming 6.2 release !

Have a nice day !!

Simon

Note on the topic if one day someone want to really support HiDPI for
retina displays :
The bug was probably introduced by this commit in Qt :
https://qt.gitorious.org/qt/qtbase/commit/1caa0c023f4fa60446094e53f22ee79771130e2f
Documentation :
https://developer.apple.com/library/mac/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/CapturingScreenContents/CapturingScreenContents.html


-- 
------------------------------------------------------------------
Simon Esneault
Rennes, France
------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150202/53662482/attachment.html>


More information about the vtkusers mailing list