[vtkusers] User defined interactor on QVTK Widget

Hughes, Mike msh at cmrl.wustl.edu
Tue Jun 3 16:48:51 EDT 2008


I'm trying to write a Qt4/VTK program that uses a user defined interactor to
read mouse position when left and right mouse buttons are pressed in the
window.  I can't get the interactor to work.  I get either a blue screen with
the text "HELLO WORLD" (as expected) but with no interaction with the mouse,
or a grey window with no text and no evidence that mouse interactions are
captured

 

I think these are the relevant lines:

 

      // interactor->SetRenderWindow(renderWindow); <-- Window is grey not
blue and has no text

      widget->SetRenderWindow(renderWindow);       // Window is blue and has
text "Hello World" but interactor does not work

 

What is the correct way to set up the user defined interactor?

 

 

 

#include <QApplication>

#include <QMainWindow>

#include <QVTKWidget.h>

#include <vtkRenderWindow.h>

#include <vtkRenderer.h>

#include <vtkTextSource.h>

#include <vtkVectorText.h>

#include <vtkPolyDataMapper.h>

#include <vtkActor.h>

 

#include "vtkInteractorStyle.h"

#include "vtkCallbackCommand.h"

#include "vtkTextMapper.h"

#include "vtkActor2D.h"

#include "vtkTextProperty.h"

 

QVTKInteractor *interactor;

 

void PickerInteractionCallback

( 

      vtkObject* vtkNotUsed(object),

      unsigned long event,

      void* clientdata,

      void* vtkNotUsed(calldata)

)

{

      int X, Y,  *pick;

      switch( event )

      {

            case vtkCommand::LeftButtonPressEvent:

                  interactor -> GetMousePosition(&X, &Y);

                  cout << "Left Mouse Button Mouse Pressed at (x,y) = ("<< X
<< ", " << Y << ")" << endl;

                  break;

            case vtkCommand::RightButtonReleaseEvent:

                  interactor -> GetMousePosition(&X, &Y);

                  cout << "Right Mouse Button Mouse Pressed at (x,y) = ("<< X
<< ", " << Y << ")" << endl;

                  break;

      }

      //vtkWidget->GetRenderWindow()->Render();

}

 

int main( int argc, char **argv )

 

{

      QApplication app(argc,argv);

      QMainWindow *mainWindow = new QMainWindow;

      mainWindow->setFixedSize(640,360);

      QVTKWidget *widget = new QVTKWidget;

      mainWindow->setCentralWidget(widget);

 

      vtkTextMapper *textMapperForMode;

      vtkActor2D *textActorForMode;

      static char Modetext[120];

 

      // Create a text mapper and actor to display HELLO WORLD

      textMapperForMode = vtkTextMapper::New();

      vtkTextProperty *tpropForMode = textMapperForMode->GetTextProperty();

      tpropForMode->SetFontFamilyToArial();

      tpropForMode->SetFontSize(12);

      tpropForMode->BoldOn();

      tpropForMode->ShadowOn();

      tpropForMode->SetColor(1, 1, 0);

      textActorForMode = vtkActor2D::New();

      textActorForMode->VisibilityOn();

      textActorForMode->SetMapper(textMapperForMode);

      sprintf( Modetext, "HELLO WORLD");

      textMapperForMode->SetInput( Modetext ); 

      textActorForMode->SetPosition(10,10);

      textActorForMode->VisibilityOn();

 

      vtkInteractorStyle *style = vtkInteractorStyle::New();

      vtkCallbackCommand * pickerCommand = vtkCallbackCommand::New();

      pickerCommand->SetCallback(PickerInteractionCallback);

      style->AddObserver(vtkCommand::RightButtonPressEvent, pickerCommand);

      style->AddObserver(vtkCommand::LeftButtonPressEvent, pickerCommand);

 

      vtkRenderer *renderer = vtkRenderer::New();

      renderer->SetBackground(0.0,0.0,1.0);

      renderer->AddActor2D(textActorForMode);

      

      vtkRenderWindow *renderWindow = vtkRenderWindow::New();

      renderWindow->AddRenderer(renderer);

      interactor=QVTKInteractor::New();

      interactor->SetInteractorStyle(style);

 

      // interactor->SetRenderWindow(renderWindow); <-- Window is grey not
blue and has no text

      widget->SetRenderWindow(renderWindow);       // Window is blue and has
text "Hello World" but interactor does not work

 

      mainWindow->show();

      

      return app.exec();

 

}

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20080603/a0afeb6a/attachment.htm>


More information about the vtkusers mailing list