graphics view 2

29
Qt Essentials - Graphics View 2 Module Qt Essentials - Training Course Produced by Nokia, Qt Development Frameworks Material based on Qt 4.7, created on December 15, 2010 http://qt.nokia.com 1/29

Upload: nishantapatil

Post on 06-Mar-2015

129 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Graphics View 2

Qt Essentials - Graphics View 2 ModuleQt Essentials - Training Course

Produced by Nokia, Qt Development Frameworks

Material based on Qt 4.7, created on December 15, 2010

http://qt.nokia.com

1/29

Page 2: Graphics View 2

Module: Graphics View 2Widgets in a SceneDrag and DropEffectsPerformance Tuning

Graphics View 2 2/29

Page 3: Graphics View 2

Module: Graphics View 2

Widgets in a SceneDrag and DropEffectsPerformance Tuning

Graphics View 2 Widgets in a Scene 3/29

Page 4: Graphics View 2

Widgets in a Scene

Demo $QTDIR/examples/graphicsview/padnavigator

Graphics View 2 Widgets in a Scene 4/29

Page 5: Graphics View 2

Items are not widgets

• QGraphicsItem:• Lightweight compared to QWidget• No signals/slots/properties• Scenes can easily contain thousands of Items• Uses different QEvent sub-hierarchy (derived fromQGraphicsSceneEvent)

• Supports transformations directly

• QWidget:• Derived from QObject (less light-weight)• supports signals, slots, properties, etc• can be embedded in a QGraphicsScene with aQGraphicsProxyWidget

Graphics View 2 Widgets in a Scene 5/29

Page 6: Graphics View 2

QGraphicsWidget

• Advanced functionality graphics item• Provides signals/slots, layouts, geometry, palette, etc.• Not a QWidget!• Base class for QGraphicsProxyWidget

Graphics View 2 Widgets in a Scene 6/29

Page 7: Graphics View 2

QGraphicsProxyWidget

• QGraphicsItem that can embed a QWidget in aQGraphicsScene

• Handles complex widgets like QFileDialog

• Takes ownership of related widget• Synchronizes states/properties:

• visible, enabled, geometry, style, palette, font, cursor,sizeHint, windowTitle, etc

• Proxies events between Widget and GraphicsView• If either (widget or proxy) is deleted, the other is also!

• Widget must not already have a parent• Only top-level widgets can be added to a scene

Graphics View 2 Widgets in a Scene 7/29

Page 8: Graphics View 2

Embedded Widget Example

#include <QtGui>int main(int argc, char **argv) {

QApplication app(argc, argv);

QCalendarWidget *calendar = new QCalendarWidget;

QGraphicsScene scene;QGraphicsProxyWidget *proxy = scene.addWidget(calendar);

QGraphicsView view(&scene);view.show();

return app.exec();}

Graphics View 2 Widgets in a Scene 8/29

Page 9: Graphics View 2

QGraphicsLayout

• For layout of QGraphicsLayoutItem (+derived) classes inQGraphicsView

• Concrete classes:• QGraphicsLinearLayout: equivalent to QBoxLayout, arranges

items horizontally or vertically• QGraphicsGridLayout: equivalent to QGridLayout, arranges

items in a grid

• QGraphicsWidget::setLayout() - set layout for child itemsof this QGraphicsWidget

Graphics View 2 Widgets in a Scene 9/29

Page 10: Graphics View 2

Lab: Widgets in a Scene

• Starting with the graphicsview/lab-mapviewer handout,add zooming controls.

• Suggested widgets:• QPushButtons for +/-• QSlider for selecting zoom level directly.

• Use QGraphicsLayout to lay out the widgets• Make the mouse work like a "hand-grab" tool on drag, so we

can see different zoomed areas.

Graphics View 2 Widgets in a Scene 10/29

Page 11: Graphics View 2

Module: Graphics View 2

Widgets in a SceneDrag and DropEffectsPerformance Tuning

Graphics View 2 Drag and Drop 11/29

Page 12: Graphics View 2

Drag and Drop

• Items can be:• Dragged• Dropped onto other items• Dropped onto scenes

• for handling empty drop areas

Graphics View 2 Drag and Drop 12/29

Page 13: Graphics View 2

Start Drag

Starting an item drag is similar to dragging from a QWidget.• Override event handlers:

• mousePressEvent()• mouseMoveEvent()

• In mouseMoveEvent(), decide if drag started? if so:• Create a QDrag instance• Attach a QMimeData to it

• See section on Drag and Drop for QMimeData info

• Call QDrag::exec()

• Function returns when user drops• Does not block event loop

Graphics View 2 Drag and Drop 13/29

Page 14: Graphics View 2

Drop on a scene

• Override QGraphicsScene::dropEvent()

• To accept drop:• acceptProposedAction()• setDropAction(Qt::DropAction); accept();

• Override QGraphicsScene::dragMoveEvent()

• Optional overrides:• dragEnterEvent(), dragLeaveEvent()

Graphics View 2 Drag and Drop 14/29

Page 15: Graphics View 2

startDrag example

void startDrag( Qt::DropActions supportedActions ) {QList<QListWidgetItem *> items = selectedItems();if ( items.size()>0 ) {

QDrag* drag = new QDrag( this );QMimeData *mimeData = new QMimeData; [...]QGraphicsItem* gitem =

DiagramItem::createItem( item->toolType() );mimeData->setData( "application/x-qgraphicsitem-ptr",

QByteArray::number( ( qulonglong )gitem ) );drag->setMimeData( mimeData );QPixmap pix = item->icon().pixmap( 111,111 );drag->setPixmap( pix );drag->setHotSpot( pix.rect().center() );if ( drag->exec(supportedActions) == Qt::IgnoreAction ) {

delete gitem; // drag cancelled, must delete item}

Demo graphicsview/ex-dragdrop

Graphics View 2 Drag and Drop 15/29

Page 16: Graphics View 2

dropEvent() on a scene

void DiagramScene::dropEvent( QGraphicsSceneDragDropEvent* event ) {if (event->mimeData()->hasFormat("application/x-qgraphicsitem-ptr")) {

QGraphicsItem* item = reinterpret_cast<QGraphicsItem*>(event->mimeData()->data ("application/x-qgraphicsitem-ptr").toULongLong() );

if ( item ) {addItem( item );item->setFlag( QGraphicsItem::ItemIsMovable );item->setFlag( QGraphicsItem::ItemIsSelectable );item->setFlag( QGraphicsItem::ItemIsFocusable );item->setPos( event->scenePos() );event->acceptProposedAction();

}} else

/* Call baseclass to allow per-item dropEvent */

QGraphicsScene::dropEvent( event );}

Demo graphicsview/ex-dragdropGraphics View 2 Drag and Drop 16/29

Page 17: Graphics View 2

Drop on an item

• To drop into an item:• Override dragEnterEvent()• Optional override: dragMoveEvent() (if the item can only accept

drops in some parts of its area)void DiagramItem::dragEnterEvent(QGraphicsSceneDragDropEvent* e){

if ( e->mimeData()->hasColor() )e->acceptProposedAction();

}

void DiagramScene::dragMoveEvent(QGraphicsSceneDragDropEvent* e){if (e->mimeData()->hasFormat("application/x-qgraphicsitem-ptr"))

e->acceptProposedAction();else

QGraphicsScene::dragMoveEvent(e);}

Demo graphicsview/ex-dragdrop

Graphics View 2 Drag and Drop 17/29

Page 18: Graphics View 2

Module: Graphics View 2

Widgets in a SceneDrag and DropEffectsPerformance Tuning

Graphics View 2 Effects 18/29

Page 19: Graphics View 2

Graphics Effects

Effects can be applied to graphics items:

• Base class for effects is QGraphicsEffect.• Standard effects include blur, colorize, opacity and drop

shadow.• Effects are set on items.

• QGraphicsItem::setGraphicsEffect()

• Effects cannot be shared or layered.• Custom effects can be written.

Graphics View 2 Effects 19/29

Page 20: Graphics View 2

Using a Graphics Effect

• Applying a blur effect to a pixmap.

QPixmap pixmap(":/images/qt-banner.png");

QGraphicsItem *blurItem = scene->addPixmap(pixmap);

QGraphicsBlurEffect *blurEffect = new QGraphicsBlurEffect();blurItem->setGraphicsEffect(blurEffect);blurEffect->setBlurRadius(5);

• An effect is owned by the item that uses it.• Updating an effect causes the item to be updated.

Graphics View 2 Effects 20/29

Page 21: Graphics View 2

Module: Graphics View 2

Widgets in a SceneDrag and DropEffectsPerformance Tuning

Graphics View 2 Performance Tuning 21/29

Page 22: Graphics View 2

Level of Detail• Don’t draw what you can’t see!• QStyleOptionGraphicsItem passed to paint()

• Contains palette, state, matrix members• qreal levelOfDetailFromTransform(QTransform T) method

• "levelOfDetail" is max width/height of the unity rectangleneeded to draw this shape onto a QPainter with aQTransform of T.

• use worldTransform() of painter for current transform.• Zoomed out: levelOfDetail < 1.0• Zoomed in: levelOfDetail > 1.0

Graphics View 2 Performance Tuning 22/29

Page 23: Graphics View 2

Level of detail: Chip demo

Demo $QTDIR/demos/chip

Graphics View 2 Performance Tuning 23/29

Page 24: Graphics View 2

Level of detail: Chip demo 2

void Chip::paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *)

{const qreal lod = option->levelOfDetailFromTransform(

painter->worldTransform());[ ... ]if (lod >= 2) {

QFont font("Times", 10);font.setStyleStrategy(QFont::ForceOutline);painter->save();painter->setFont(font);painter->scale(0.1, 0.1);painter->drawText(170, 180, QString("Model: VSC-2000 ..."painter->drawText(170, 220, QString("Manufacturer: ..."painter->restore();

}

Demo $QTDIR/demos/chip

Graphics View 2 Performance Tuning 24/29

Page 25: Graphics View 2

Caching tips

• Cache item painting into a pixmap• So paint() runs faster

• Cache boundingRect() and shape()• Avoid recomputing expensive operations that stay the same• Be sure to invalidate manually cached items after zooming and

other transforms

QRectF MyItem::boundingRect() const {if (m_rect.isNull()) calculateBoundingRect();return m_rect;

}

QPainterPath MyItem::shape() const {if (m_shape.isEmpty()) calculateShape();return m_shape;

}

Graphics View 2 Performance Tuning 25/29

Page 26: Graphics View 2

setCacheMode()

• Property of QGraphicsView and QGraphicsItem

• Allows caching of pre-rendered content in a QPixmap

• Drawn on the viewport• Especially useful for gradient shape backgrounds• Invalidated whenever view is transformed.

QGraphicsView view;view.setBackgroundBrush(QImage(":/images/backgroundtile.png"));view.setCacheMode(QGraphicsView::CacheBackground);

Graphics View 2 Performance Tuning 26/29

Page 27: Graphics View 2

Tweaking

The following methods allow you to tweak performance ofview/scene/items:• QGraphicsView::setViewportUpdateMode()

• QGraphicsView::setOptimizationFlags()

• QGraphicsScene::setItemIndexMethod()

• QGraphicsScene::setBspTreeDepth()

• QGraphicsItem::setFlags()

• ItemDoesntPropagateOpacityToChildren andItemIgnoresParentOpacity especially recommended if youritems are opaque!

See API documentation for details.

Graphics View 2 Performance Tuning 27/29

Page 28: Graphics View 2

Tips for better performance

• boundingRect() and shape() are called frequently so theyshould run fast!• boundingRect() should be as small as possible• shape() should return simplest reasonable path

• Try to avoid drawing gradients on the painter. Consider usingpre-rendered backgrounds from images instead.

• It is costly to dynamically insert/remove items from the scene.Consider hiding and reusing items instead.

• Embedded widgets in a scene is costly.• Try using a different paint engine (OpenGL, Direct3D, etc)

• setViewport (new QGLWidget);

• Avoid curved and dashed lines• Alpha blending and antialiasing are expensive

Graphics View 2 Performance Tuning 28/29

Page 29: Graphics View 2

c© 2010 Nokia Corporation and its Subsidiary(-ies).

The enclosed Qt Training Materials are provided under the CreativeCommons Attribution ShareAlike 2.5 License Agreement.

The full license text is available here:http://creativecommons.org/licenses/by-sa/2.5/legalcode

Nokia, Qt and the Nokia and Qt logos are the registered trademarks ofNokia Corporation in Finland and other countries worldwide.

Graphics View 2 Legal 29/29