Qt的橡皮筋類

https://www.jianshu.com/p/5f20e2cd3ee0
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setBackgroundRole(QPalette::Light);
    this->setAutoFillBackground(true);

    resize(400,360);
    setWindowTitle("Rubber");
    rubberBand = NULL;
}

Widget::~Widget()
{

}

// QPoint origin;
void Widget::mousePressEvent(QMouseEvent *e)
{
    origin = e->pos();
    if(!rubberBand){
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    }
    rubberBand->setGeometry(QRect(origin, QSize()));//設置了橡皮筋線的位置及大小
    rubberBand->show();
}

void Widget::mouseMoveEvent(QMouseEvent *e)
{
    if(rubberBand)
           rubberBand->setGeometry(QRect(origin,e->pos()).normalized());
    //normalized() 函數返回的也是一個QRect的對象,不過該對象的長和寬的值都是大於零時值
}
void Widget::mouseReleaseEvent(QMouseEvent *e)
{
//    if(rubberBand)
//          rubberBand->hide();  //當鼠標鬆開時,橡皮筋線就能夠隱藏了
}
相關文章
相關標籤/搜索