Qt--QRubberBand橡皮筋類

轉自qt中橡皮筋類 QRubberBand

在圖形編輯應用中常會用到橡皮筋線,如選擇圖形的某個區(qū)域等,最常見的就是在系統(tǒng)桌面上用鼠標拖動,可以繪制一個類似螞蟻線的選區(qū),并且選區(qū)線能夠跟隨鼠標的移動而伸縮,因此叫作橡皮筋線。
qt 中用于描繪橡皮筋線的類是QRubberBand,當然單有一個QRubberBand 類還是不能做出橡皮筋的效果出來, 另外還要有鼠標事件的配合,與QRubberBand 配合的鼠標事件有 鼠標單擊,拖動及釋放。
創(chuàng)建一個QRubberBand 類,new QRubberBand(QRubberBand::Line,this)
QRubberBand::Rectangle 是設置橡皮筋線的類型,這種線的效果是描繪了一個方形的區(qū)域,還有** QRubberBand::Line** ,則為一個被直線填滿的方形區(qū)域,相當于一個陰影的方形區(qū)域。
QRubberBand 應用最多的函數是 setGeometry(),其作用是設置了橡皮筋線的位置及大小。
自己定義一個橡皮筋的類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;
}
//構造函數完成了對窗體尺寸及背景的設置。

//鼠標在窗體中按下時,創(chuàng)建一個QRubberBand 類,QRubberBand::Rectangle 是設置橡皮筋線的類型,
//這種線的效果是描繪了一個方形的區(qū)域,還有一種是QRubberBand::Line,則為一個被直線填滿的方形區(qū)域,
//相當于一個陰影的方形區(qū)域。QRubberBand 應用最多的函數是 setGeometry(),其作用是設置了橡皮筋線的位置及大小。
void Rubber::mousePressEvent(QMouseEvent *e)
{
    origin = e->pos();
    if(!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Line,this);
    rubberBand->setGeometry(QRect(origin,QSize()));
    rubberBand->show();
}

//在鼠標按下,并且鼠標發(fā)生移動的時候,這時就可以會出橡皮線的區(qū)域,
//鼠標拖動事件函數重載如下 改區(qū)域的大小由QRect(origin,e->pos()).normalized()) 來體現,
//其中normalized() 函數返回的也是一個QRect的對象,不過該對象的長和寬的值都是大于零時值
void Rubber::mouseMoveEvent(QMouseEvent *e)
{
    if(rubberBand)
        rubberBand->setGeometry(QRect(origin,e->pos()).normalized());
}

//當鼠標松開時,橡皮筋線就可以隱藏了
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;
}
//構造函數完成了對窗體尺寸及背景的設置。

//鼠標在窗體中按下時,創(chuàng)建一個QRubberBand 類,QRubberBand::Rectangle 是設置橡皮筋線的類型,
//這種線的效果是描繪了一個方形的區(qū)域,還有一種是QRubberBand::Line,則為一個被直線填滿的方形區(qū)域,
//相當于一個陰影的方形區(qū)域。QRubberBand 應用最多的函數是 setGeometry(),其作用是設置了橡皮筋線的位置及大小。
void Rubber::mousePressEvent(QMouseEvent *e)
{
    origin = e->pos();
    if(!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Line,this);
    rubberBand->setGeometry(QRect(origin,QSize()));
    rubberBand->show();
}

//在鼠標按下,并且鼠標發(fā)生移動的時候,這時就可以會出橡皮線的區(qū)域,
//鼠標拖動事件函數重載如下 改區(qū)域的大小由QRect(origin,e->pos()).normalized()) 來體現,
//其中normalized() 函數返回的也是一個QRect的對象,不過該對象的長和寬的值都是大于零時值
void Rubber::mouseMoveEvent(QMouseEvent *e)
{
    if(rubberBand)
        rubberBand->setGeometry(QRect(origin,e->pos()).normalized());
}

//當鼠標松開時,橡皮筋線就可以隱藏了
void Rubber::mouseReleaseEvent(QMouseEvent *e)
{
    if(rubberBand)
        rubberBand->hide();
}

Line.png
Rectangle.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容