Qt 实现在窗口内拖动图片

Qt在窗口内拖动图片

label中显示图片,把label放在QScrollArea中,当窗口内显示图片,当图片大小超过窗口时出现滚动条,当鼠标点击左键在窗口内移动时,窗口的滚动条和内容随鼠标移动而变化。

安装过滤器,对鼠标事件进行处理。并对滚动条进行属性设置。

头文件:

 

#ifndef SCROLLAREA_H
#define SCROLLAREA_H

#include 
#include 

class ScrollArea : public QScrollArea
{
    Q_OBJECT

public:
    ScrollArea(QWidget* parent =NULL);
    ~ScrollArea();

protected:
    bool eventFilter(QObject *obj, QEvent *evt);

 private:
    bool mMoveStart;
    bool mContinuousMove;
    QPoint mMousePoint;

};
#endif // SCROLLAREA_H

源文件:

#include "scrollarea.h"
#include 
#include 
#include 

ScrollArea::ScrollArea(QWidget* parent)
    : QScrollArea(parent)
    , mMoveStart(false)
{
    installEventFilter(this);
    horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal"
                                         "{"
                                         "height:8px;"
                                         "background:rgba(0,0,0,0%);"
                                         "margin:0px,0px,0px,0px;"
                                         "padding-left:0px;"
                                         "padding-right:0px;"
                                         "}"
                                         "QScrollBar::handle:horizontal"
                                         "{"
                                         "height:8px;"
                                         "background:rgba(0,0,0,25%);"
                                         " border-radius:4px;"
                                         "min-height:20;"
                                         "}"
                                         "QScrollBar::handle:horizontal:hover"
                                         "{"
                                         "height:8px;"
                                         "background:rgba(0,0,0,50%);"
                                         " border-radius:4px;"
                                         "min-height:20;"
                                         "}"
                                         "QScrollBar::add-line:horizontal"
                                         "{"
                                         "height:0px;width:0px;"
                                         "subcontrol-position:bottom;"
                                         "}"
                                         "QScrollBar::sub-line:horizontal"
                                         "{"
                                         "height:0px;width:0px;"
                                         "subcontrol-position:top;"
                                         "}"
                                         "QScrollBar::add-page:horizontal,QScrollBar::sub-page:horizontal"
                                         "{"
                                         "background:rgba(0,0,0,10%);"
                                         "border-radius:4px;"
                                         "}"
                                         );
    verticalScrollBar()->setStyleSheet("QScrollBar:vertical"
                                       "{"
                                       "width:8px;"
                                       "background:rgba(0,0,0,0%);"
                                       "margin:0px,0px,0px,0px;"
                                       "padding-top:0px;"
                                       "padding-bottom:0px;"
                                       "}"
                                       "QScrollBar::handle:vertical"
                                       "{"
                                       "width:8px;"
                                       "background:rgba(0,0,0,25%);"
                                       " border-radius:4px;"
                                       "min-height:20;"
                                       "}"
                                       "QScrollBar::handle:vertical:hover"
                                       "{"
                                       "width:8px;"
                                       "background:rgba(0,0,0,50%);"
                                       " border-radius:4px;"
                                       "min-height:20;"
                                       "}"
                                       "QScrollBar::add-line:vertical"
                                       "{"
                                       "height:0px;width:0px;"
                                       "subcontrol-position:bottom;"
                                       "}"
                                       "QScrollBar::sub-line:vertical"
                                       "{"
                                       "height:0px;width:0px;"
                                       "subcontrol-position:top;"
                                       "}"
                                       "QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical"
                                       "{"
                                       "background:rgba(0,0,0,10%);"
                                       "border-radius:4px;"
                                       "}"
                                       );
}

ScrollArea::~ScrollArea()
{

}

bool ScrollArea::eventFilter(QObject *obj, QEvent *evt)
{
    if (evt->type() == QEvent::MouseMove) {
        QMouseEvent* mouseEvent = (QMouseEvent*) evt;
        if ((mouseEvent->buttons() & Qt::LeftButton)) {
            if (!mMoveStart) {
                mMoveStart = true;
                mContinuousMove =false;
                mMousePoint = mouseEvent->globalPos();
            }

            else {
                QPoint mousePoint = mouseEvent->globalPos();

                int x_offset = mousePoint.y() - mMousePoint.y();
                int y_offset = mousePoint.x() - mMousePoint.x();
                mContinuousMove = true;

                setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//开启滚动条
                setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

                QScrollBar *vScrollBar=verticalScrollBar();
                QScrollBar *hScrollBar=horizontalScrollBar();

                vScrollBar->setValue(vScrollBar->value() - x_offset);
                hScrollBar->setValue(hScrollBar->value() - y_offset);

                mMousePoint = mousePoint;
            }
            return true;
        }
    }
    else if (evt->type() == QEvent::MouseButtonRelease) {
        mMoveStart = false;
        setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//关闭滚动条
        setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    }

    return QObject::eventFilter(obj, evt);
}


 

 

你可能感兴趣的:(Qt)