QT: implementation of irregular windows

Source: Internet
Author: User
Tags transparent image

Main ideas:
1: Set the form to Qt::framelesswindowhint (remove the title bar).
2: Use a part of the area is transparent image as the interface of the program, and the picture is transparent to the place to penetrate.

3: The mouse event of the overloaded program.

Runtime (light green is the desktop background)

Source:

[CPP]View Plaincopyprint?
    1. #include <QtGui>
    2. Class Irregularwidget: Public qwidget
    3. {
    4. Q_object
    5. Public
    6. Irregularwidget (Qwidget *parent = 0);
    7. Protected
    8. void Mousepressevent (Qmouseevent *event);
    9. void Mousemoveevent (Qmouseevent *event);
    10. void PaintEvent (Qpaintevent *event);
    11. void Enterevent (Qevent *event);
    12. void Leaveevent (Qevent *event);
    13. Private
    14. Qpoint M_currentpos;
    15. Qpixmap M_pixmap;
    16. };
    17. Irregularwidget::irregularwidget (Qwidget *parent)
    18. : Qwidget (parent, Qt::framelesswindowhint)
    19. {
    20. Setwindowtitle ("Irregular widget");
    21. //Load a picture that has some area that is transparent as a program interface
    22. M_pixmap.load ("delete.png");
    23. Resize (m_pixmap.size ());
    24. ///Irregular window key, make the picture transparent place to penetrate
    25. Setmask (M_pixmap.mask ());
    26. }
    27. void Irregularwidget::mousepressevent (Qmouseevent *event)
    28. {
    29. //Press the left button to move the window, press the right button to close the program
    30. if (event->button () = = Qt::leftbutton)
    31. {
    32. M_currentpos = Event->globalpos ()-Framegeometry (). TopLeft ();
    33. Event->accept ();
    34. }
    35. Else if (event->button () = = Qt::rightbutton)
    36. Close ();
    37. }
    38. void Irregularwidget::mousemoveevent (Qmouseevent *event)
    39. {
    40. if (event->buttons () && Qt::leftbutton)
    41. {
    42. Move (Event->globalpos ()-m_currentpos);
    43. Event->accept ();
    44. }
    45. }
    46. void Irregularwidget::p aintevent (qpaintevent *event)
    47. {
    48. qpainter painter (this);
    49. Painter.drawpixmap (0, 0, m_pixmap);
    50. }
    51. void Irregularwidget::leaveevent (Qevent *event)
    52. {
    53. //The mouse is a normal pointer when it leaves the window
    54. SetCursor (Qt::arrowcursor);
    55. }
    56. void Irregularwidget::enterevent (Qevent *event)
    57. {
    58. //Mouse is a finger when left on the window
    59. SetCursor (Qt::P ointinghandcursor);
    60. }
    61. #include "Main.moc"
    62. int main (int argc, char *argv[])
    63. {
    64. Qapplication app (argc, argv);
    65. Irregularwidget *widget = new Irregularwidget;
    66. Widget->show ();
    67. return app.exec ();
    68. }

http://blog.csdn.net/small_qch/article/details/7054750

QT: implementation of irregular 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.