[QT programming] scaling and dragging of borderless windows

Source: Internet
Author: User
Currently, the vast majority of software is moving towards simplicity and fashion. Taking youdao's word book and my own word book as an example, most users will certainly like the single-word book I have made (on the UI alone, don't worry about color matching and layout issues ).

Youdao's word base:


The statement I have made:


Obviously, the main difference between the two is the border problem. You can compare the previous versions of QQ with those of recent years, and you will find that they all prefer the following window mode. Let's talk about how to use QT to zoom and drag a borderless window.
Dragging a window without borders is actually very simple. The basic idea is to record the coordinates of the mouse before and after moving the mouse, and then move the distance between the two coordinates, you can see the code for the specific implementation. The following describes how to change the size of a window. First, we divide a window into the following nine areas. Only when the mouse is in Area 22 cannot change its shape or size. When the mouse is in another area, the mouse changes the shape and the window size. Window areas are classified as follows:


The specific implementation code is as follows (widget. UI has not been changed ): 1. widget. h file
# Ifndef widget_h # define widget_h # include <qwidget> # define margin 20 // length of the four corners namespace UI {class widget;} class Widget: Public qwidget {q_object public: explicit widget (qwidget * parent = 0 );~ Widget (); int countflag (qpoint P, int row); void setcursortype (INT flag); int countrow (qpoint P); protected: void mousepressevent (qmouseevent * event ); void mousereleaseevent (qmouseevent * event); void mousedoubleclickevent (qmouseevent * event); void mousemoveevent (qmouseevent * event); Private: Ui: widget * UI; bool isleftpressed; int curpos; qpoint plast ;};# endif // widget_h

2. widget. cpp File

# Include "widget. H "# include" ui_widget.h "# include <qmouseevent> # include <qdebug> Widget: widget (qwidget * parent): qwidget (parent), UI (new UI: widget) {UI-> setupui (this); this-> setmousetracking (true); // you can set to trigger the mouse movement event when the mouse is not pressed. Pay attention to qmainwindow: centralwidget ()-> setmousetracking (true); isleftpressed = false; curpos = 0; // mark the position when the mouse is left-clicked this-> setminimumsize (400,300 ); // set the minimum qcursor cursor and cursor. setshape (QT: ARRO Wcursor); // set the cursor to the arrow shape. // UI-> Pushbutton-> setcursor (cursor); // The Arrow when the cursor is placed on the button. // cursor. setshape (QT: openhandcursor); qwidget: setcursor (cursor); // when placed on the main window, it is a hand-shaped qdebug () <"H =" <this-> height (); setwindowflags (QT: framelesswindowhint); // you can specify qdebug () for the main window without borders () <this-> minimumheight ();} Widget ::~ Widget () {Delete UI;} void Widget: mousepressevent (qmouseevent * event) // click the event {If (Event-> button () = QT: leftbutton) {This-> isleftpressed = true; qpoint temp = event-> globalpos (); plast = temp; curpos = countflag (Event-> pos (), countrow (Event-> pos (); event-> ignore () ;}} void Widget: mousereleaseevent (qmouseevent * event) // The mouse release event {If (isleftpressed) isleftpressed = false; qapplication: restoreoverridecursor (); // restore Re-mouse pointer properties event-> ignore ();} void Widget: mousedoubleclickevent (qmouseevent * event) // double-click the full screen with the mouse {If (Event-> button () = QT:: leftbutton) {If (windowstate ()! = QT: windowfullscreen) setwindowstate (QT: windowfullscreen); else setwindowstate (QT: windownostate); // restore normal mode} event-> ignore ();} void Widget:: mousemoveevent (qmouseevent * event) // The mouse movement event {int poss = countflag (Event-> pos (), countrow (Event-> pos ())); setcursortype (poss); If (isleftpressed) // whether to left-click {qpoint ptemp = event-> globalpos (); ptemp = ptemp-plast; If (curpos = 22) // move the window {ptemp = ptemp + pos (); move (ptemp);} els E {qrect WID = geometry (); Switch (curpos) // change the window size {Case 11: wid. settopleft (WID. topleft () + ptemp); break; // Case 13: WID in the upper left corner. settopright (WID. topright () + ptemp); break; // case 31: WID in the upper right corner. setbottomleft (WID. bottomleft () + ptemp); break; // case 33: WID in the lower left corner. setbottomright (WID. bottomright () + ptemp); break; // case 12: WID in the lower right corner. settop (WID. top () + ptemp. Y (); break; // medium case 21: wid. setleft (WID. left () + ptemp. X (); break; // center left ca Se 23: wid. setright (WID. right () + ptemp. X (); break; // case 32: wid. setbottom (WID. bottom () + ptemp. Y (); break; // lower and lower corners} setgeometry (WID);} plast = event-> globalpos (); // update location} event-> ignore ();} int Widget: countflag (qpoint P, int row) // calculates the column and row where the mouse is located {If (P. Y () <margin) return 10 + row; else if (P. Y ()> This-> height ()-margin) return 30 + row; else return 20 + row;} void Widget: setcursortype (INT flag) // change the shape of the mouse pointer Based on the mouse position {Qt: cursorshape cursor; Switch (FLAG) {Case 11: Case 33: cursor = QT: sizefdiagcursor; break; Case 13: Case 31: cursor = QT: sizebdiagcursor; break; Case 21: Case 23: cursor = QT: sizehorcursor; break; Case 12: Case 32: cursor = QT: sizevercursor; break; Case 22: cursor = QT :: openhandcursor; break; default: // qapplication: restoreoverridecursor (); // restores the mouse pointer character break;} setcursor (cursor);} int Widget: countrow (qpoin T p) // in which column is the calculation {return (p. x () <margin )? 1 :( p. x ()> (this-> width ()-margin )? 3: 2 );}
3. Main. cpp File

#include<QtWidgets>#include "widget.h"int main(int argc, char *argv[]){    QApplication a(argc, argv);    Widget w;    w.show();        return a.exec();}

The program runs as follows:


When you place the mouse on the edge of the window, the shape of the mouse changes, indicating that you can drag the window. Because the window is not closed, the window can only be forcibly closed. If you want to minimize and close the painting of different windows, you can place two toolbuttons in the upper left corner of the window, set the autorise attribute, and add a piece. The following shows the main interface of the dictionary software using the above borderless window:




[QT programming] scaling and dragging of borderless windows

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.