用QT实现类似QQ截图的工具

#include "widget.h"
#include "ui_widget.h"
#include <QtGui>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    createWidgets();
    createConnects();
    createEventFilter();
}

Widget::~Widget()
{
    delete ui;

delete quit;
    delete mini;
    delete restore;
    delete menu;
    delete trayIcon;

delete fullScreenLabel;
    delete shotScreenLabel;
}

bool Widget::eventFilter(QObject *o, QEvent *e)
{
    if (o != fullScreenLabel)
    {
        return Widget::eventFilter(o, e);
    }

QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (e);

//true 鼠标左键按下且按键还未弹起
    if ((mouseEvent->button() == Qt::LeftButton)
        && (mouseEvent->type() == QEvent::MouseButtonPress))
    {
        //鼠标左键标志位按下
        leftMousePress = true;

//获取鼠标点
        origin = mouseEvent->pos();

if (!rubberBand)
        {
            rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
        }

rubberBand->setGeometry(QRect(origin,QSize()));
        rubberBand->show();

return true;
    }

//true 鼠标左键按下并拖动
    if ((mouseEvent->type() == QEvent::MouseMove)
        && (leftMousePress))
    {
        if (rubberBand)
        {
            rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized());
        }

return true;
    }

//鼠标左键松开
    if ((mouseEvent->button() == Qt::LeftButton)
        && (mouseEvent->type() == QEvent::MouseButtonRelease))
    {
        //鼠标标志位弹起
        leftMousePress = false;

if (rubberBand)
        {
            //获取橡皮筋框的终止坐标
            termination = mouseEvent->pos();
            QRect rect = QRect(origin, termination);

//根据橡皮筋框截取全屏上的信息,并将其放入shotScreenLabel
            shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,
                                                                   rect.x(),
                                                                   rect.y(),
                                                                   rect.width(),
                                                                   rect.height()));

//将shotScreenLabel的用户区大小固定为所截图片大小
            shotScreenLabel->setFixedSize(rect.width(), rect.height());
            shotScreenLabel->show();

rubberBand->hide();
            fullScreenLabel->hide();
        }

return true;
    }

return false;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxppd.html