You are on page 1of 35

VTK Scratches

A. Costa 2013
VTK & QT
You must turn on the following options in the advanced VTK
build configuration:
• Follow the link:
• http://www.vtk.org/Wiki/VTK/Tutorials/QtSetup

Install the latest Qt SDK


And build VTK (RELEASE MODE)

With VTK 5.x:


VTK_USE_QT
VTK_USE_GUISUPPORT
BUILD_SHARED_LIBS
QT
The Meta-Object System
It provides the signals and slots mechanism for inter-
object communication, run-time type information, and
the dynamic property system.
• The QObject class provides a base class for objects
that can take advantage of the meta-object system.
• The Q_OBJECT macro inside the private section of the
class declaration is used to enable meta-object
features, such as dynamic properties, signals, and
slots.
• The Meta-Object Compiler (moc) supplies each
QObject subclass with the necessary code to
implement meta-object features.
QObject &
QMetaObject
QObject::metaObject() returns the associated meta-
object for the class.
QMetaObject::className() returns the class name as a
string at run-time, without requiring native run-time type
information (RTTI) support through the C++ compiler.
QObject::inherits() function returns whether an object is
an instance of a class that inherits a specified class
within the QObject inheritance tree.
QObject::tr() and QObject::trUtf8() translate strings for
internationalization.
QObject::setProperty() and QObject::property()
dynamically set and get properties by name.
QMetaObject::newInstance() constructs a new instance
of the class.
“MOC”
• The Meta-Object Compiler, moc, is the
program that handles Qt's C++ extensions.
• The moc tool reads a C++ header file.
• If it finds all the the Q_OBJECT macros and it
produces a C++ source file containing the
meta-object code for those classes.
• The meta-object code is required for the
o signals and slots mechanism,
o the run-time type information, and the
o dynamic property system.
Qmake
Let's assume that you have just finished a basic
implementation of your application, and you have
created the following files:

• hello.cpp
• hello.h
• main.cpp
Qmake the .pro file
hello.pro file

*************
CONFIG += qt
HEADERS += hello.h
Variables
SOURCES += hello.cpp \
main.cpp
*************

Generate a makefile with the following command:


qmake -o Makefile hello.pro
Qmake the .pro file
hello.pro file
The “qt” value will add
he relevant libraries to be
************* linked against and ensure that
CONFIG += qt build lines for moc and uic are
included in the generated
HEADERS += hello.h
Makefile.
SOURCES += hello.cpp \
main.cpp
*************

Generate a makefile with the following command:


qmake -o Makefile hello.pro
Make the application debugable

*************
CONFIG += qt debug
HEADERS += hello.h
SOURCES = hello.cpp \
main.cpp
*************
Adding Platform-Specific Source Files

*******************
win32 {
SOURCES += hellowin.cpp
}
***********************

A more comprehensive list of platform scope values:


MAC WIN32 UNIX
Nested scopes:
*******************
win32:debug {
CONFIG += console
}
***********************

Boolean expressions:
***********************
!exists( main.cpp ) {
error( "No main.cpp file found" )
}
***********************
Common Projects

• Application
• Library
• Plugin
The app template
TEMPLATE = app

The app template tells qmake to generate a Makefile


that will build an application.

CONFIG variable definition:

windows The application is a Windows GUI


application.
console app template only: the application is a
Windows console application.
The app template
When using this template the following qmake system
variables are recognized. You should use these in your .pro
file to specify information about your application.

HEADERS - A list of all the header files for the application.


SOURCES - A list of all the source files for the application.
FORMS - A list of all the UI files (created using Qt Designer) for
the application.
LEXSOURCES - A list of all the lex source files for the
application.
YACCSOURCES - A list of all the yacc source files for the
application.
TARGET - Name of the executable for the application. This
defaults to the name of the project file. (The extension, if any,
is added automatically).
The app template
DESTDIR - The directory in which the target executable is
placed.
DEFINES - A list of any additional pre-processor defines
needed for the application.
INCLUDEPATH - A list of any additional include paths needed
for the application.
DEPENDPATH - The dependency search path for the
application.
VPATH - The search path to find supplied files.
DEF_FILE - Windows only: A .def file to be linked against for
the application.
RC_FILE - Windows only: A resource file for the application.
RES_FILE - Windows only: A resource file to be linked against
for the application.
The lib template
The lib template tells qmake to generate a Makefile
that will build a library.

TEMPLATE = lib

VERSION variable is supported.

CONFIG variable definition:


Option Description
dll The library is a shared library (dll).
staticlib The library is a static library.
plugin The library is a plugin; this also enables the
dll option.
A Standard C++ project
building process
The QT project building
process

The MOC compiler will process each file


containing the Q_OBJECT macro !
The QT project building
process
Some information
• Events and slots
MVC
• The QT way ….
CMake
cmake_minimum_required(VERSION 2.8)
PROJECT(VTKScratches)

FIND_PACKAGE(Qt4 REQUIRED)
FIND_PACKAGE(VTK)

INCLUDE(${VTK_USE_FILE})
INCLUDE(${QT_USE_FILE})

INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR} )

SET(VTKScratchesSrcs Main.cxx VTKScratches.cxx MainPanel.cpp)


SET(VTKScratchesUI VTKScratches.ui MainPanel.ui)
SET(VTKScratchesHeaders VTKScratches.h MainPanel.h)
CMake
To invoke moc, the macro QT4_WRAP_CPP is used. It assigns the
names of the resulting files to the variable listed first

QT4_WRAP_CPP(MOCSrcs ${VTKScratchesHeaders} )
QT4_WRAP_UI(UISrcs ${VTKScratchesUI})
CMake
Finally, CMake needs to know the name of the resulting executable and what to
link it to.

ADD_EXECUTABLE( VTKScratches ${VTKScratchesSrcs} ${UISrcs} ${MOCSrcs})


TARGET_LINK_LIBRARIES( VTKScratches QVTK )
A first example...
main.cxx
#include <QApplication>
#include "VTKScratches.h"

int main(int argc, char* argv[])


{
QApplication app( argc, argv );

///Create the VTKScratches Main Window and execute it...


VTKScratches simpleVTK_MainWindow;
simpleVTK_MainWindow.show();

return app.exec();
}
VTKScratches.h
#ifndef VTKScratches_H
#define VTKScratches_H
#include <vtkSmartPointer.h>
#include <QMainWindow>
#include "MainPanel.h"

class Ui_VTKScratches;
class VTKScratches : public QMainWindow
{
Q_OBJECT
public:
// Constructor/Destructor
VTKScratches();
~VTKScratches();
void addMainPanel();
public slots:
void on_actionAbout_triggered();
void on_actionExit_2_triggered();
private:
Ui_VTKScratches *ui;
MainPanel* m_mainPanel;
};
#endif
What is VTK
• Data-flow driven paradigm
Implicit Objects:

• Pipeline / graph oriented Lights


Camera
• Data
• Filter (Reader & Writer)
• Mapper
• Actor
• Renderers
• RenderWindow
• RenderWindowInteractor
Reader
Objects Mapper

Data

Filter 1 Actor

Data

Filter 2 It receives one


Renderer
or more actors

It is the visualized
windows RenderWindow

Mapper
It gives the
RenderWindowInteractor
interactivity

Actor
VTK Memory Management
• One way to create a VTK object is

vtkObject* MyObject = vtkObject::New();

• You must manually delete the object


MyObject->Delete();
VTK Pointer Classes
• http://www.kitware.com/source/home/post/7

• http://www.vtk.org/Wiki/Smart_Pointers

• The idea behind smart pointers is reference counting. If


the object goes out of scope and it is not being used
anywhere else, it will be deleted automatically.

vtkSmartPointer<vtkActor> sphereActor =vtkSmartPointer<vtkActor>::New();

• The special vtkSmartPointer<>::New() method creates an


object and smart pointer in one step, with the pointer
holding the initial ownership of the object
Designer
File-> New-> Templates->MainWindow
Designer
Qt 4 classes for main window
• QMainWindow is the central class around which
applications can be built

• QDockWidget provides a widget that can be used to


create detachable tool palettes or helper windows.
Dock widgets keep track of their own properties, and
they can be moved, closed, and floated as external
windows.

• QToolBar provides a generic toolbar widget that can


hold a number of different action-related widgets, such
as buttons, drop-down menus, comboboxes, and spin
boxes
QAction

You might also like