Before doing about the content of the Qdockwidget encountered a bottleneck, that is, the window will not drag after the bounce, nor can zoom in and zoom out, if it is set to the window after the flags, can not be dragged, but also not the effect of the need.
1. The titlebar right side of the dockwidget will need to have 3 buttons to control the amplification and recovery, bounce out and close button. Considering the QT comes out of the dockwidget can not achieve this, so refer to the online method, you need to derive a class from the qwidget to achieve their own titlebar
2. Since dockwidget is nested in Qtabwidget, and Tabwidget itself is not draggable, this dockwiget also cannot be dragged (the drag of the Qt neutron window is associated with the parent window), Plus the need to implement your own dockwidget style, so also derive a class from the Qdockwidget
First realize your own titlebar:
1 classMydockwidgettitlebar: PublicQwidget2 {3 Q_object4 5 Private:6 //the class member definition that you need7Qsize Sizehint ()Const{returnminimumsizehint ();}8Qsize Mimimumsizehint ()Const;//when writing your own titlebar, these 2 functions must be implemented9 Ten protected: One voidPaintEvent (Qpaintevent *Event); A voidMousedoubleclickevent (Qmouseevent *Event); - voidMousereleaseevent (Qmouseevent *Event); - the PrivateSlots: - //some of the slot functions that I've defined, like I need to zoom in, zoom out, float, and close . - voidmaxmindockwidget (); - voidfloatdockwidget (); + voidclosedockwidget (); - + Public: AMydockwidgettitlebar (Qwidget *parent =0); at~Mydockwigettitlebar (); -};
It is important to note that if Dockwidget does not have titlebar then it will settitlebarwidget directly (Qwidget ()), so that there is only one dockwidget window and no titlebar on the window. (The method from the Internet, the source address can not be found, if you find and add the original link)
Here's a look at the implementation of a function that must be implemented when implementing TitleBar:
1Qsize *mydockwidgettitlebar::minimumsizehint ()Const2 {3Mydockwidget*dock_widget = Qobject_cast<mydockwidget *>(Parentwidget ());4Q_assert (Dock_widget! =0);5Qsize result ( -, -);6 if(Dock_widget->features &qdockwidget::D ockwidgetverticaltitlebar)7 {8 result.transpose ();9 }Ten returnresult; One}
PaintEvent is primarily used to draw icons and positions on titlebar. But I'm going to use these buttons elsewhere, so I've defined the buttons as class members and given the type of the button in the constructor. Other slots functions can be defined on a demand basis.
To achieve your own dockwidget:
1 classMydockwidget: PublicQdockwidget2 {3 Q_object4 5 Private:6 //Some class member variables7 int_cur_point_pos;8 int_margin_length;9Qwidget *_default_title_bar;TenMydockwidgettitlebar *_dock_widget_title_bar; One A protected: - voidMousemoveevent (Qmouseevent *Event);//because you want to implement the drag, this mouse move event must be implemented - voidMousepressevent (Qmouseevent *Event);//drag when the left mouse button should be pressed, so need to judge in this the voidMousereleaseevent (Qmouseevent *Event); - voidCloseevent (Qcloseevent *Event); - - Public: +Mydockwidget (Qtabwidget *tab_widget, qwidget *parent =0); -~mydockwidget (); +};
It is important to note that Q_object is used in the class definitions here, and if there is a definition of the signal and slot functions in the class, you must write this, for specific reasons refer to the QT help manual.
The above class also needs to define a Boolean variable to determine if the left mouse button has been pressed and the position of the mouse before clicking:
1 Private : 2 BOOL _left_mose_press; 3 Qpoint _last_point_pos, _length_pos;
In Mouse press events:
1 voidMydockwidget::mousepressevent (Qmouseevent *Event)2 {3 if(Event->button = =Qt::leftbutton)4 {5_left_mouse_press =ture;6_last_point_pos =Event-Globalpos ();7_cur_point_pos = Countflag (Event->pos (), Countcolumn (Event-POS () ));8 Event-ignore ();9 } Ten}
In the mouse release event:
1 voidMydockwidget::mousereleaseevent (Qmouseevent *Event)2 {3 if(_left_mouse_press)4 {5_left_mouse_press =false;6 }7 qapplication::restoreoverridecursor ();8 Event-ignore (); 9}
The focus is here on the mouse movement event, the method here refers to the "QT Programming" QT-based Dictionary development series < four >--borderless window zoom and drag, and here is a very detailed description: There is a drag window is also available when the mouse to four weeks to display different shapes to zoom out. Here is a good way to divide a window into 3*3 's plate.
But here is a little bad is that every time you drag the time will be redrawn, if the contents of the window is more complex, when the mouse moved too fast when the window will appear in a position to stop. Further improvements are needed here.
(There are a number of ways to move the window itself, but given that to support different platforms, it may not be possible to use only the functions under Windows, so the above method is chosen, if only the Windows platform, consider the following method:
1. qt window drag and change size
2. Qt Untitled No Border program drag and change the size
3. "Qt" mobile borderless form
These are some of the methods I found at that time, in fact, most of the methods on the Internet are similar, of course, if you can see the example of a demo, you will find that in fact, QT source code also has a similar operation, the online method and QT examples are the same.
Of course, QT also has a function to get the current screen size, this can be used to deal with zoom in and zoom out of the time to see what size:
1 qrect desktop_rect = qapplication::d esktop ()->availablegeometry ();
In Qt, when Qdockwidget's parent window is a qtabwidget that cannot be dragged, the method of dragging is implemented