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
- # Ifndef slidingwindow_h
- # Define slidingwindow_h
- # Include <qtgui>
- Class slidingwindow: Public qwidget
- {
- Q_object
- Public:
- Slidingwindow (qwidget * parent = 0 );
- ~ Slidingwindow ();
- Protected:
- Void mousepressevent (qmouseevent * event );
- Void mousemoveevent (qmouseevent * event );
- Void mousereleaseevent (qmouseevent * event );
- Void resizeevent (qresizeevent * event );
- Bool eventfilter (qobject * OBJ, qevent * event );
- PRIVATE:
- Void setbtnpos ();
- Void setbtnicon ();
- Private slots:
- Void slotclickedbtn ();
- Void slotsplittermoved (INT POs, int index );
- PRIVATE:
- Qpoint dragposition;
- Bool bpressflag;
- Qsplitter * splitter;
- Qframe * contentframe;
- Qframe * listframe;
- Qpushbutton * Pushbutton;
- Qicon lefticon;
- Qicon righticon;
- };
- # Endif // slidingwindow_h
(2) slidingwindow. cpp
[HTML]View Plain Copy
- # Include "slidingwindow. H"
- Slidingwindow: slidingwindow (qwidget * parent)
- : Qwidget (parent, QT: framelesswindowhint)
- , Bpressflag (false)
- {
- Resize (660,460 );
- Splitter = new qsplitter (QT: horizontal, this );
- Splitter-> sethandlewidth (1 );
- Splitter-> setstylesheet ("qsplitter: handle {Background: # ffffff }");
- Contentframe = new qframe (splitter );
- Contentframe-> setstylesheet ("Background: #000000 ");
- Contentframe-> resize( 475, height ());
- Contentframe-> setminimumwidth (405 );
- Listframe = new qframe (splitter );
- Listframe-> setstylesheet ("Background: #323232 ");
- Listframe-> resize (660-475, height ());
- Listframe-> setmaximumwidth (660-405 );
- Connect (splitter, signal (splittermoved (INT, INT), this, slot (slotsplittermoved (INT, INT )));
- Lefticon = qicon (":/left.png ");
- Righticon = qicon (":/right.png ");
- Pushbutton = new qpushbutton (this );
- Pushbutton-> setfocuspolicy (QT: nofocus );
- Pushbutton-> hide ();
- Pushbutton-> setfixedsize (13, 42 );
- Pushbutton-> seticonsize (Pushbutton-> size ());
- Pushbutton-> setstylesheet ("border: none ;");
- Pushbutton-> seticon (righticon );
- Pushbutton-> move (contentframe-> width ()-Pushbutton-> width ()-2, (contentframe-> height ()-Pushbutton-> height ()/2 );
- Connect (Pushbutton, signal (clicked (), this, slot (slotclickedbtn ()));
- Contentframe-> setmousetracking (true );
- Listframe-> setmousetracking (true );
- Contentframe-> installeventfilter (this );
- Listframe-> installeventfilter (this );
- }
- Slidingwindow ::~ Slidingwindow ()
- {
- }
- Void slidingwindow: resizeevent (qresizeevent * event)
- {
- Splitter-> setgeometry (0, 0, width (), height ());
- Move (qapplication: desktop ()-> width ()-width ()/2, (qapplication: desktop ()-> height ()-height ()) /2 );
- Qwidget: resizeevent (event );
- }
- Bool slidingwindow: eventfilter (qobject * OBJ, qevent * event)
- {
- If (Event-> type () = qevent: mousemove ){
- Qmouseevent * mousemove = static_cast <qmouseevent *> (event );
- Qrect rect = Pushbutton-> framegeometry ();
- If (rect. Contains (mousemove-> pos ())){
- Pushbutton-> show ();
- }
- Else {
- Pushbutton-> hide ();
- }
- }
- Return qwidget: eventfilter (OBJ, event );
- }
- Void slidingwindow: setbtnpos ()
- {
- Pushbutton-> move (contentframe-> width ()-Pushbutton-> width (), (contentframe-> height ()-Pushbutton-> height ()/2 );
- }
- Void slidingwindow: setbtnicon ()
- {
- If (listframe-> width ()! = 0 ){
- Pushbutton-> seticon (righticon );
- }
- Else {
- Pushbutton-> seticon (lefticon );
- }
- }
- Void slidingwindow: slotclickedbtn ()
- {
- Qlist <int> sizelist;
- Sizelist. Clear ();
- If (listframe-> width ()! = 0 ){
- Sizelist. append (660 );
- Sizelist. append (0 );
- }
- Else {
- Sizelist. append (475 );
- Sizelist. append (660-475 );
- }
- Splitter-> setsizes (sizelist );
- Setbtnicon ();
- Setbtnpos ();
- Pushbutton-> hide ();
- }
- Void slidingwindow: slotsplittermoved (INT POs, int index)
- {
- Q_unused (POS)
- Q_unused (INDEX)
- Setbtnicon ();
- Setbtnpos ();
- }
- Void slidingwindow: mousepressevent (qmouseevent * event)
- {
- Bpressflag = true;
- Dragposition = event-> pos ();
- Qwidget: mousepressevent (event );
- }
- Void slidingwindow: mousemoveevent (qmouseevent * event)
- {
- If (bpressflag ){
- Qpoint relapos (qcursor: pos ()-dragposition );
- Move (relapos );
- }
- Qwidget: mousemoveevent (event );
- }
- Void slidingwindow: mousereleaseevent (qmouseevent * event)
- {
- Bpressflag = false;
- Qwidget: mousereleaseevent (event );
- }
(3) Main. cpp
[HTML]View Plain Copy
- # Include "slidingwindow. H"
- # Include <qapplication>
- Int main (INT argc, char * argv [])
- {
- Qapplication A (argc, argv );
- Slidingwindow W;
- W. Show ();
- Return a.exe C ();
- }
(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