Archived:Using QRubberband in Qt
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): Qt
Article
Keywords: QRubberBand,QMouseEvent
Created: mind_freak
(05 Aug 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
The QRubberBand class provides a rectangle or line that can indicate a selection or a boundary.A rubber band is often used to show a new bounding area.
QRubberBand is generally used when we want to select multiple items at a time.
This example makes the use of following classes:
QRubberBand-Provides a rectangle or line that can indicate a selection or a boundary
Code Snippet
Header file
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QDesktopWidget>
#include <QApplication>
#include <QPixmap>
#include <QRubberBand>
#include <QMouseEvent>
#include <QPoint>
#include <QRect>
#include <QSize>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
private:
QRubberBand *rubberBand;
QPoint mypoint;
};
#endif // WIDGET_H
cpp File
#ifndef WIDGET_H
#define WIDGET_H
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
}
Widget::~Widget()
{
}
void Widget::mousePressEvent(QMouseEvent *event)
{
mypoint = event->pos();
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);//new rectangle band
rubberBand->setGeometry(QRect(mypoint, QSize()));
rubberBand->show();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(mypoint, event->pos()).normalized());//Area Bounding
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();// hide on mouse Release
}


23 Sep
2009
Hamishwillee - Not going to delete
Ikipou 12:59, 6 September 2009 (UTC) posted:
I agree that the code is partially broken. I don't agree that a rubber band is pointless - on larger touch screens devices it might be very useful.
In any case this is archived, because we're recommending Qt Quick for this sort of stuff.hamishwillee 03:10, 14 February 2012 (EET)