Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
QGraphicsPixmapItem example 5: respond to cursor moving over pixmap is a
QGraphicsPixmapItem
example. This example shows how to let the QGraphicsPixmapItem emit
a (Boost) signal so that the QGraphicsView can rotate it. It is usefull
to be able to send signals to the QGraphicsView, because QGraphicsView
is a QObject, where QGraphicsPixmapItem is not. Due to this,
the item cannot work with Qt signals, but the
view can (note that QGraphicsObject is a
QGraphicsItem that can work with signals and slots).
QGraphicsPixmapItem example 5: respond to cursor moving over pixmap is the predecessor of
QGraphicsPixmapItem example 6: let the view respond to cursor moving over pixmap, which is
the predecessor of QGraphicsPixmapItem example 7: respond to cursor moving over pixmap while holding the cursor still.
Technical facts
Application type(s)
Operating system(s) or programming environment(s)
IDE(s):
Project type:
C++ standard:
Compiler(s):
Libraries used:
Qt: version 5.4.1 (32 bit)
STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppQGraphicsPixmapItemExample5/CppQGraphicsPixmapItemExample5.pro
./CppQGraphicsPixmapItemExample5/qtitem.h
./CppQGraphicsPixmapItemExample5/qtitem.cpp
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <cassert>
#include <QCursor>
#include <QPainter>
#include "qtitem.h"
#pragma GCC diagnostic pop
QtItem::QtItem(QGraphicsItem *parent)
: QGraphicsPixmapItem(parent),
m_signal_move_over{}
{
assert(this->pixmap().isNull()
&& "Assume no pixmap loaded yet");
//Load the pixmap from resources
this->setPixmap(QPixmap(":/images/PicR.png"));
assert(!this->pixmap().isNull()
&& "Assume pixmap is loaded successfully");
//Let the item have (0.0,0.0) as its center,
//so it will remain in place when rotating
this->setOffset(-pixmap().rect().center());
this->setFlags(
QGraphicsItem::ItemIsMovable
| QGraphicsItem::ItemIsSelectable
);
//Without this line, hoverMoveEvent will not be called
this->setAcceptHoverEvents(true);
}
void QtItem::hoverMoveEvent(QGraphicsSceneHoverEvent *)
{
//Emit the boost signal
m_signal_move_over(this);
}
void QtItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//Let QGraphicsPixmapItem handle most of the painting
QGraphicsPixmapItem::paint(painter,option,widget);
//Draw a thick red line around the pixmap when it is selected
if (this->isSelected())
{
painter->setPen(QPen(QColor(255,0,0),3));
painter->drawRoundedRect(this->boundingRect().adjusted(3.0,3.0,-3.0,-3.0),6.0,6.0);
}
}
|
./CppQGraphicsPixmapItemExample5/qtmain.cpp
./CppQGraphicsPixmapItemExample5/qtwidget.h
./CppQGraphicsPixmapItemExample5/qtwidget.cpp
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.

This page has been created by the tool CodeToHtml