关闭

Qt 实现在窗口内拖动图片

标签: Qt QScrollAreadrag动作拖动图片
1541人阅读 评论(0) 收藏 举报
分类:

Qt在窗口内拖动图片

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

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

头文件:

 

#ifndef SCROLLAREA_H
#define SCROLLAREA_H

#include <QScrollArea>
#include <QPoint>

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 <QMouseEvent>
#include <QScrollBar>
#include <QDebug>

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);
}


 

 

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26833次
    • 积分:677
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:3篇
    • 译文:0篇
    • 评论:6条
    最新评论