Qt discussion on the qsplitter Implementation of freely scalable sliding windows

Source: Internet
Author: User
I. Introduction

Recently, I saw an article about qt's window for scaling and sliding, but its code is incomplete. Then I wrote the complete code and made it open-source. A slider in the center of the window can dynamically change the size of the subwindow, and hidden buttons can be used to quickly scale the subwindow. It is as follows:

Ii. Details 1. Code

(1) slidingwindow. h

[HTML]View Plain Copy
  1. # Ifndef slidingwindow_h
  2. # Define slidingwindow_h
  3. # Include <qtgui>
  4. Class slidingwindow: Public qwidget
  5. {
  6. Q_object
  7. Public:
  8. Slidingwindow (qwidget * parent = 0 );
  9. ~ Slidingwindow ();
  10. Protected:
  11. Void mousepressevent (qmouseevent * event );
  12. Void mousemoveevent (qmouseevent * event );
  13. Void mousereleaseevent (qmouseevent * event );
  14. Void resizeevent (qresizeevent * event );
  15. Bool eventfilter (qobject * OBJ, qevent * event );
  16. PRIVATE:
  17. Void setbtnpos ();
  18. Void setbtnicon ();
  19. Private slots:
  20. Void slotclickedbtn ();
  21. Void slotsplittermoved (INT POs, int index );
  22. PRIVATE:
  23. Qpoint dragposition;
  24. Bool bpressflag;
  25. Qsplitter * splitter;
  26. Qframe * contentframe;
  27. Qframe * listframe;
  28. Qpushbutton * Pushbutton;
  29. Qicon lefticon;
  30. Qicon righticon;
  31. };
  32. # Endif // slidingwindow_h

(2) slidingwindow. cpp

[HTML]View Plain Copy
  1. # Include "slidingwindow. H"
  2. Slidingwindow: slidingwindow (qwidget * parent)
  3. : Qwidget (parent, QT: framelesswindowhint)
  4. , Bpressflag (false)
  5. {
  6. Resize (660,460 );
  7. Splitter = new qsplitter (QT: horizontal, this );
  8. Splitter-> sethandlewidth (1 );
  9. Splitter-> setstylesheet ("qsplitter: handle {Background: # ffffff }");
  10. Contentframe = new qframe (splitter );
  11. Contentframe-> setstylesheet ("Background: #000000 ");
  12. Contentframe-> resize( 475, height ());
  13. Contentframe-> setminimumwidth (405 );
  14. Listframe = new qframe (splitter );
  15. Listframe-> setstylesheet ("Background: #323232 ");
  16. Listframe-> resize (660-475, height ());
  17. Listframe-> setmaximumwidth (660-405 );
  18. Connect (splitter, signal (splittermoved (INT, INT), this, slot (slotsplittermoved (INT, INT )));
  19. Lefticon = qicon (":/left.png ");
  20. Righticon = qicon (":/right.png ");
  21. Pushbutton = new qpushbutton (this );
  22. Pushbutton-> setfocuspolicy (QT: nofocus );
  23. Pushbutton-> hide ();
  24. Pushbutton-> setfixedsize (13, 42 );
  25. Pushbutton-> seticonsize (Pushbutton-> size ());
  26. Pushbutton-> setstylesheet ("border: none ;");
  27. Pushbutton-> seticon (righticon );
  28. Pushbutton-> move (contentframe-> width ()-Pushbutton-> width ()-2, (contentframe-> height ()-Pushbutton-> height ()/2 );
  29. Connect (Pushbutton, signal (clicked (), this, slot (slotclickedbtn ()));
  30. Contentframe-> setmousetracking (true );
  31. Listframe-> setmousetracking (true );
  32. Contentframe-> installeventfilter (this );
  33. Listframe-> installeventfilter (this );
  34. }
  35. Slidingwindow ::~ Slidingwindow ()
  36. {
  37. }
  38. Void slidingwindow: resizeevent (qresizeevent * event)
  39. {
  40. Splitter-> setgeometry (0, 0, width (), height ());
  41. Move (qapplication: desktop ()-> width ()-width ()/2, (qapplication: desktop ()-> height ()-height ()) /2 );
  42. Qwidget: resizeevent (event );
  43. }
  44. Bool slidingwindow: eventfilter (qobject * OBJ, qevent * event)
  45. {
  46. If (Event-> type () = qevent: mousemove ){
  47. Qmouseevent * mousemove = static_cast <qmouseevent *> (event );
  48. Qrect rect = Pushbutton-> framegeometry ();
  49. If (rect. Contains (mousemove-> pos ())){
  50. Pushbutton-> show ();
  51. }
  52. Else {
  53. Pushbutton-> hide ();
  54. }
  55. }
  56. Return qwidget: eventfilter (OBJ, event );
  57. }
  58. Void slidingwindow: setbtnpos ()
  59. {
  60. Pushbutton-> move (contentframe-> width ()-Pushbutton-> width (), (contentframe-> height ()-Pushbutton-> height ()/2 );
  61. }
  62. Void slidingwindow: setbtnicon ()
  63. {
  64. If (listframe-> width ()! = 0 ){
  65. Pushbutton-> seticon (righticon );
  66. }
  67. Else {
  68. Pushbutton-> seticon (lefticon );
  69. }
  70. }
  71. Void slidingwindow: slotclickedbtn ()
  72. {
  73. Qlist <int> sizelist;
  74. Sizelist. Clear ();
  75. If (listframe-> width ()! = 0 ){
  76. Sizelist. append (660 );
  77. Sizelist. append (0 );
  78. }
  79. Else {
  80. Sizelist. append (475 );
  81. Sizelist. append (660-475 );
  82. }
  83. Splitter-> setsizes (sizelist );
  84. Setbtnicon ();
  85. Setbtnpos ();
  86. Pushbutton-> hide ();
  87. }
  88. Void slidingwindow: slotsplittermoved (INT POs, int index)
  89. {
  90. Q_unused (POS)
  91. Q_unused (INDEX)
  92. Setbtnicon ();
  93. Setbtnpos ();
  94. }
  95. Void slidingwindow: mousepressevent (qmouseevent * event)
  96. {
  97. Bpressflag = true;
  98. Dragposition = event-> pos ();
  99. Qwidget: mousepressevent (event );
  100. }
  101. Void slidingwindow: mousemoveevent (qmouseevent * event)
  102. {
  103. If (bpressflag ){
  104. Qpoint relapos (qcursor: pos ()-dragposition );
  105. Move (relapos );
  106. }
  107. Qwidget: mousemoveevent (event );
  108. }
  109. Void slidingwindow: mousereleaseevent (qmouseevent * event)
  110. {
  111. Bpressflag = false;
  112. Qwidget: mousereleaseevent (event );
  113. }

(3) Main. cpp

[HTML]View Plain Copy
  1. # Include "slidingwindow. H"
  2. # Include <qapplication>
  3. Int main (INT argc, char * argv [])
  4. {
  5. Qapplication A (argc, argv );
  6. Slidingwindow W;
  7. W. Show ();
  8. Return a.exe C ();
  9. }

(4) Compile and run

Iii. Summary

(1) to obtain the positional coordinates, you can try qpoint realpos (qcursor: pos ()-This-> pos (); so that no type conversion is required. Setmousetracking (true) is used to move the mouse to the mousemoveevent function when the mouse is not clicked.
(2) the source code has been uploaded to csdn: http://download.csdn.net/detail/taiyang1987912/9439233.
(3) If you have any questions or suggestions, please leave a message. Thank you!

Http://blog.csdn.net/taiyang1987912/article/details/50717179

Qt discussion on the qsplitter Implementation of freely scalable sliding windows

Related Article

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.