轉(zhuǎn)自qt中橡皮筋類 QRubberBand
在圖形編輯應(yīng)用中常會用到橡皮筋線,如選擇圖形的某個區(qū)域等,最常見的就是在系統(tǒng)桌面上用鼠標(biāo)拖動,可以繪制一個類似螞蟻線的選區(qū),并且選區(qū)線能夠跟隨鼠標(biāo)的移動而伸縮,因此叫作橡皮筋線。
qt 中用于描繪橡皮筋線的類是QRubberBand,當(dāng)然單有一個QRubberBand 類還是不能做出橡皮筋的效果出來, 另外還要有鼠標(biāo)事件的配合,與QRubberBand 配合的鼠標(biāo)事件有 鼠標(biāo)單擊,拖動及釋放。
創(chuàng)建一個QRubberBand 類,new QRubberBand(QRubberBand::Line,this)
QRubberBand::Rectangle 是設(shè)置橡皮筋線的類型,這種線的效果是描繪了一個方形的區(qū)域,還有** QRubberBand::Line** ,則為一個被直線填滿的方形區(qū)域,相當(dāng)于一個陰影的方形區(qū)域。
QRubberBand 應(yīng)用最多的函數(shù)是 setGeometry(),其作用是設(shè)置了橡皮筋線的位置及大小。
自己定義一個橡皮筋的類Rubber 如下所示:
新建一個Qt空項目rubber.pro
qt += gui core
SOURCES += \
main.cpp \
rubber.cpp
HEADERS += \
rubber.h
main.cpp
#include <QtGui/QApplication>
#include "rubber.h"
int main(int argc,char**argv)
{
QApplication a(argc,argv);
Rubber rubber;
rubber.show();
return a.exec();
}
rubber.h
#ifndef RUBBER_H
#define RUBBER_H
#include <QWidget>
#include <QRubberBand>
#include <QMouseEvent>
class Rubber : public QWidget
{
Q_OBJECT
public:
explicit Rubber(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
private:
QRubberBand *rubberBand;
QPoint origin;
};
#endif // RUBBER_H
rubber.cpp
#include "rubber.h"
Rubber::Rubber(QWidget *parent) :
QWidget(parent)
{
setParent(parent);
this->setBackgroundRole(QPalette::Light);
this->setAutoFillBackground(true);
resize(400,360);
setWindowTitle("Rubber");
rubberBand = NULL;
}
//構(gòu)造函數(shù)完成了對窗體尺寸及背景的設(shè)置。
//鼠標(biāo)在窗體中按下時,創(chuàng)建一個QRubberBand 類,QRubberBand::Rectangle 是設(shè)置橡皮筋線的類型,
//這種線的效果是描繪了一個方形的區(qū)域,還有一種是QRubberBand::Line,則為一個被直線填滿的方形區(qū)域,
//相當(dāng)于一個陰影的方形區(qū)域。QRubberBand 應(yīng)用最多的函數(shù)是 setGeometry(),其作用是設(shè)置了橡皮筋線的位置及大小。
void Rubber::mousePressEvent(QMouseEvent *e)
{
origin = e->pos();
if(!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Line,this);
rubberBand->setGeometry(QRect(origin,QSize()));
rubberBand->show();
}
//在鼠標(biāo)按下,并且鼠標(biāo)發(fā)生移動的時候,這時就可以會出橡皮線的區(qū)域,
//鼠標(biāo)拖動事件函數(shù)重載如下 改區(qū)域的大小由QRect(origin,e->pos()).normalized()) 來體現(xiàn),
//其中normalized() 函數(shù)返回的也是一個QRect的對象,不過該對象的長和寬的值都是大于零時值
void Rubber::mouseMoveEvent(QMouseEvent *e)
{
if(rubberBand)
rubberBand->setGeometry(QRect(origin,e->pos()).normalized());
}
//當(dāng)鼠標(biāo)松開時,橡皮筋線就可以隱藏了
void Rubber::mouseReleaseEvent(QMouseEvent *e)
{
if(rubberBand)
rubberBand->hide();
}
#include "rubber.h"
Rubber::Rubber(QWidget *parent) :
QWidget(parent)
{
setParent(parent);
this->setBackgroundRole(QPalette::Light);
this->setAutoFillBackground(true);
resize(400,360);
setWindowTitle("Rubber");
rubberBand = NULL;
}
//構(gòu)造函數(shù)完成了對窗體尺寸及背景的設(shè)置。
//鼠標(biāo)在窗體中按下時,創(chuàng)建一個QRubberBand 類,QRubberBand::Rectangle 是設(shè)置橡皮筋線的類型,
//這種線的效果是描繪了一個方形的區(qū)域,還有一種是QRubberBand::Line,則為一個被直線填滿的方形區(qū)域,
//相當(dāng)于一個陰影的方形區(qū)域。QRubberBand 應(yīng)用最多的函數(shù)是 setGeometry(),其作用是設(shè)置了橡皮筋線的位置及大小。
void Rubber::mousePressEvent(QMouseEvent *e)
{
origin = e->pos();
if(!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Line,this);
rubberBand->setGeometry(QRect(origin,QSize()));
rubberBand->show();
}
//在鼠標(biāo)按下,并且鼠標(biāo)發(fā)生移動的時候,這時就可以會出橡皮線的區(qū)域,
//鼠標(biāo)拖動事件函數(shù)重載如下 改區(qū)域的大小由QRect(origin,e->pos()).normalized()) 來體現(xiàn),
//其中normalized() 函數(shù)返回的也是一個QRect的對象,不過該對象的長和寬的值都是大于零時值
void Rubber::mouseMoveEvent(QMouseEvent *e)
{
if(rubberBand)
rubberBand->setGeometry(QRect(origin,e->pos()).normalized());
}
//當(dāng)鼠標(biāo)松開時,橡皮筋線就可以隱藏了
void Rubber::mouseReleaseEvent(QMouseEvent *e)
{
if(rubberBand)
rubberBand->hide();
}
Line.png
Rectangle.png