qt Drag a picture inside the window:
Display the label in the picture, put the label in the Qscrollarea, when the window display picture, when the picture size exceeds the window when the scroll bar, when the left mouse button in the window to move, the window scroll bar and content changes with the mouse movement.
Install filters to handle mouse events. and set the properties of the scroll bar.
Header file:
#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
Source file:
#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 "" {"" he ight: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 () = = QEv
Ent::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);//Turn on scroll bar setverticalscrollbarpolicy (Qt::ScrollBarAlways
ON);
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);//Close scroll bar setverticalscrollbarpolicy (Qt::scrollbaralwaysoff);
} return Qobject::eventfilter (obj, evt);
}