美文网首页
Qt--QRubberBand橡皮筋类

Qt--QRubberBand橡皮筋类

作者: 寒冰豌豆 | 来源:发表于2017-03-01 12:16 被阅读0次

转自qt中橡皮筋类 QRubberBand

在图形编辑应用中常会用到橡皮筋线,如选择图形的某个区域等,最常见的就是在系统桌面上用鼠标拖动,可以绘制一个类似蚂蚁线的选区,并且选区线能够跟随鼠标的移动而伸缩,因此叫作橡皮筋线。
qt 中用于描绘橡皮筋线的类是QRubberBand,当然单有一个QRubberBand 类还是不能做出橡皮筋的效果出来, 另外还要有鼠标事件的配合,与QRubberBand 配合的鼠标事件有 鼠标单击,拖动及释放。
创建一个QRubberBand 类,new QRubberBand(QRubberBand::Line,this)
QRubberBand::Rectangle 是设置橡皮筋线的类型,这种线的效果是描绘了一个方形的区域,还有** QRubberBand::Line** ,则为一个被直线填满的方形区域,相当于一个阴影的方形区域。
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;
}
//构造函数完成了对窗体尺寸及背景的设置。

//鼠标在窗体中按下时,创建一个QRubberBand 类,QRubberBand::Rectangle 是设置橡皮筋线的类型,
//这种线的效果是描绘了一个方形的区域,还有一种是QRubberBand::Line,则为一个被直线填满的方形区域,
//相当于一个阴影的方形区域。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();
}

//在鼠标按下,并且鼠标发生移动的时候,这时就可以会出橡皮线的区域,
//鼠标拖动事件函数重载如下 改区域的大小由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;
}
//构造函数完成了对窗体尺寸及背景的设置。

//鼠标在窗体中按下时,创建一个QRubberBand 类,QRubberBand::Rectangle 是设置橡皮筋线的类型,
//这种线的效果是描绘了一个方形的区域,还有一种是QRubberBand::Line,则为一个被直线填满的方形区域,
//相当于一个阴影的方形区域。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();
}

//在鼠标按下,并且鼠标发生移动的时候,这时就可以会出橡皮线的区域,
//鼠标拖动事件函数重载如下 改区域的大小由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

相关文章

  • Qt--QRubberBand橡皮筋类

    转自qt中橡皮筋类 QRubberBand 在图形编辑应用中常会用到橡皮筋线,如选择图形的某个区域等,最常见的就是...

  • CRectTracker(橡皮筋)类的使用

    https://blog.csdn.net/zb774095236/article/details/8422982...

  • 橡皮筋上的“蜗牛 ”

    实验材料 ∶橡皮筋 “蜗牛”(手机指环扣) 实验步骤 ∶1.把“蜗牛”放入橡皮筋里,拉长橡皮筋 2.当我慢慢松开...

  • 指甲油

    我在电视上看到了一个指甲油与橡皮筋的实验。 于是从家里翻出来几瓶指甲油。滴在橡皮筋上。有的一滴倒橡皮筋上橡皮筋就瞬...

  • 驯服你的思想2——day330

    策略四:橡皮筋魔术 在你的手腕上套一个橡皮筋。 当你看到橡皮筋的时候,停下来注意你的思想。 假如你此时陷入了消极的...

  • 今天我去买橡皮筋,买多了,妈妈和姐姐都骂了我,她们说我太奢侈了。

    问题1:我今天为什么要去买橡皮筋呢?因为家里没有橡皮筋扎头发了,跑步的时候都得披着头发跑。 问题2:我买橡皮筋为什...

  • SBS橡皮筋厂家是如何控制橡皮筋的质量。

    橡皮筋具有伸长率大、回弹性好、价低、使用方便等特点。你知道橡皮筋厂家是怎么样测试其质量的吗?下面由TPR橡皮筋材...

  • 人犯傻的时候啥事干不出来?

    右手: 一本正经地拿起一根橡皮筋。 左手: 挑选出橡皮筋上面最亮的一颗珠子,紧捏住,然后用力拉扯,使橡皮筋绷紧到最...

  • 橡皮筋

    小时候 橡皮筋被玩出了花样 在无数个欢声笑语中 一根又一根 你拉着这边 我拉着那边 串起了你我的童年 长大了 时间...

  • 橡皮筋

    橡皮筋这东西,不用的时候到处都是,一旦要用,一个都找不到 男人同理

网友评论

      本文标题:Qt--QRubberBand橡皮筋类

      本文链接:https://www.haomeiwen.com/subject/ofeqgttx.html