Qt: Implementation of irregular windows

Source: Internet
Author: User
Tags transparent image

Main ideas:
1: Set the form to QT: frameless=whint (remove the title bar ).
2: Use a transparent image in some areas as the program interface, and set the transparency of the image as penetration.

3: The mouse event of the reload program.

Runtime (the desktop background is light green)

Source code:

# Include <qtgui> class irregularwidget: Public qwidget {q_object public: irregularwidget (qwidget * parent = 0); protected: void mousepressevent (qmouseevent * event ); void mousemoveevent (qmouseevent * event); void paintevent (qpaintevent * event); void enterevent (qevent * event); void leaveevent (qevent * event); Private: qpoint m_currentpos; qpixmap m_pixmap;}; irregularwidget: irregularwidget (qwidget * parent): qwidget (parent, QT: framelesswindowhint) {setwindowtitle ("irregular widget "); // load a transparent image in some areas as the program interface m_pixmap.load ("delete.png"); resize (m_pixmap.size (); // key to irregular windows, set the transparent part of the image to penetrate setmask (m_pixmap.mask ();} void irregularwidget: mousepressevent (qmouseevent * event) {// press and hold the left button to hold the window, right-click to close the program if (Event-> button () = QT: leftbutton) {m_currentpos = event-> globalpos ()-framegeometry (). topleft (); event-> Accept ();} else if (Event-> button () = QT: rightbutton) Close ();} void irregularwidget :: mousemoveevent (qmouseevent * event) {If (Event-> buttons () & QT: leftbutton) {move (Event-> globalpos ()-m_currentpos ); event-> Accept () ;}} void irregularwidget: paintevent (qpaintevent * event) {qpainter painter (this); painter. drawpixmap (0, 0, m_pixmap);} void irregularwidget: leaveevent (qevent * event) {// when the mouse leaves the window, it is a common pointer setcursor (QT: arrowcursor );} void irregularwidget: enterevent (qevent * event) {// when the mouse stays on the window, it is a finger setcursor (QT: pointinghandcursor);} # include "Main. MOC "int main (INT argc, char * argv []) {qapplication app (argc, argv); irregularwidget * widget = new irregularwidget; widget-> show (); return app.exe C ();}

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.