×
Namespaces

Variants
Actions

Archived:Using QRubberband in Qt

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: 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.

Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): Qt
Symbian
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
 
}

Screenshot

Rubberband.JPG


Rubberband1.JPG

This page was last modified on 11 October 2012, at 04:15.
134 page views in the last 30 days.