QT window Untitled is moved on the desktop and cannot be moved outside the viewable range

Source: Internet
Author: User

The following is the source code of the base class, which inherits the desired moving window class from the base class.

Header file:

/************************************************************************//*basewidget.h                                                          *//*************** /#ifndef Basewidget_h#define Basewidget_h#include < Qwidget>class basewidget:public qwidget{q_objectpublic:basewidget (qwidget *parent = 0); ~BaseWidget ();p rotected: void Mousepressevent (qmouseevent *event); void Mousemoveevent (qmouseevent *event); void Mousereleaseevent (QMouseEvent *event); bool m_moving;//is used to mark whether the mouse moves qpoint m_offset;private:}; #endif//Basewidget_h

CPP file:

/************************************************************************//* BaseWidget.cpp *//************************************************************************/#include "BaseWidget.h "#include <QMouseEvent> #include <QDesktopWidget> #include <qapplication>basewidget::basewidget ( Qwidget *parent): Qwidget (Parent,qt::framelesswindowhint), m_moving (false) {}basewidget::~basewidget () {}void Basewidget::mousepressevent (qmouseevent *event) {if ((Event->button () = = Qt::leftbutton)) {m_moving = True;m_offset = Event->pos ();}} void Basewidget::mousemoveevent (Qmouseevent *event) {if (m_moving) {//method 1:qdesktopwidget* Desktop = qapplication:: Desktop (); Qrect Windowrect (Desktop->availablegeometry ()); Qrect Widgetrect (This->geometry ()); Qpoint Point (Event->globalpos ()-m_offset);//The following is to prevent the window from being dragged out of visible range outside//left if (point.x () <= 0) {point = Qpoint (0,point.y ());} Right int y = Windowrect.bottomright (). Y ()-this->size (). height ();F (Point.y () >= y && widgetrect.topleft (). Y () >= y) {point = Qpoint (Point.x (), y);} Upper if (point.y () <= 0) {point = Qpoint (Point.x (), 0);} the bottom int x = Windowrect.bottomright (). x ()-This->size (). width (); if (Point.x () >= x && widgetrect.topleft () . x () >= x) {point = Qpoint (X,point.y ());} Move (point),///method 2://can be determined by Qrect Windowrect whether it contains (contains) Qrect widgetrect to move//Here is no code}//if just ask to move the window, with the following code can be implemented// Move (Event->globalpos ()-m_offset);} void Basewidget::mousereleaseevent (qmouseevent*event) {if (Event->button () = = Qt::leftbutton) m_moving = false;}



QT window Untitled is moved on the desktop and cannot be moved outside the viewable range

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.