[vtkusers] setSlice problem on QVTKWidget

Massi Massinissa.Bandou at USherbrooke.ca
Thu Sep 6 21:37:29 EDT 2012


Hi,
Originally the code works perfectly without Qt. I can't tell you if Qt is a
part of the problem or not but I guess my problem was the fact that I'm
using smartpointer will delete the track of the objects (ImageViewer) it
points to after calling my function.

this is the shortest code to read a dicom series and display them on
QVTKWidget.

#include <vtkSmartPointer.h>
#include <vtkObjectFactory.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkActor.h>
#include <vtkImageViewer2.h>
#include <vtkDICOMImageReader.h>
#include <vtkInteractorStyleImage.h>
#include <sstream>
#include <QWidget>
#include <QMainWindow>
 
// Define own interaction style
class myVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
   myVtkInteractorStyleImage(): ImageViewer(NULL){};
   static myVtkInteractorStyleImage* New();
   vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);
 
protected:
   vtkImageViewer2* ImageViewer;
   int Slice;
   int MinSlice;
   int MaxSlice;
 
public:
   void SetImageViewer(vtkImageViewer2* imageViewer) {
      ImageViewer = imageViewer;
      MinSlice = imageViewer->GetSliceMin();
      MaxSlice = imageViewer->GetSliceMax();
      Slice = MinSlice;
      cout << "Slicer: Min = " << MinSlice << ", Max = " << MaxSlice <<
std::endl;
   }
 
protected:
   void MoveSliceForward() {
      if(Slice < MaxSlice) {
         Slice += 1;
         cout << "MoveSliceForward::Slice = " << Slice << std::endl;
         ImageViewer->SetSlice(Slice);
         ImageViewer->Render();
      }
   }
 
   void MoveSliceBackward() {
      if(Slice > MinSlice) {
         Slice -= 1;
         cout << "MoveSliceBackward::Slice = " << Slice << std::endl;
         ImageViewer->SetSlice(Slice);
         ImageViewer->Render();
      }
   }

   virtual void OnKeyDown() {
      std::string key = this->GetInteractor()->GetKeySym();
      if(key.compare("Up") == 0) {
         MoveSliceForward();
      }
      else if(key.compare("Down") == 0) {
         MoveSliceBackward();
      }
   }

};
 
vtkStandardNewMacro(myVtkInteractorStyleImage);

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    QMainWindow mainWindow;
    mainWindow.setGeometry(100, 100, 200, 200);

    // Verify input arguments
    if ( argc != 2 )
    {
      std::cout << "Usage: " << argv[0]
      << " FolderName" << std::endl;
      return EXIT_FAILURE;
    }
 
    std::string folder = argv[1];
    //std::string folder = "C:\\VTK\\vtkdata-5.8.0\\Data\\DicomTestImages";
 
    // Read all the DICOM files in the specified directory.
    vtkSmartPointer<vtkDICOMImageReader> reader =
      vtkSmartPointer<vtkDICOMImageReader>::New();
    reader->SetDirectoryName(folder.c_str());
    reader->Update();
 
    // Visualize
    vtkSmartPointer<vtkImageViewer2> imageViewer =
      vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputConnection(reader->GetOutputPort());

    vtkSmartPointer<myVtkInteractorStyleImage> myInteractorStyle =
      vtkSmartPointer<myVtkInteractorStyleImage>::New();
 
    myInteractorStyle->SetImageViewer(imageViewer);
    
    QVTKWidget *myWidget = new QVTKWidget;

    myWidget->SetRenderWindow(imageViewer->GetRenderWindow());
   
myWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle(myInteractorStyle);
    imageViewer->Render();
    myWidget->update();
  
   mainWindow.show();
   return app.exec();
}

Thank you

Massi



--
View this message in context: http://vtk.1045678.n5.nabble.com/setSlice-problem-on-QVTKWidget-tp5715880p5715919.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list