Qt implementation Drag a picture inside the window

Source: Internet
Author: User
Tags bool min
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);
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.