The following is an example of a dock window I wrote:
The following code implements the above window design:
1. When a project is created, select qmainwindow as the base class and deselect the select status of the gernerate form check box.
2. Write the following code in the source file mainwindow. cpp:
# Include "mainwindow. H "# include <qtextedit> # include <qdockwidget> mainwindow: mainwindow (qwidget * parent): qmainwindow (parent) {// set the main window setwindowtitle (TR ("dockwindwos"); qtextedit * TE = new qtextedit (this); Te-> settext (TR ("Main Window ")); te-> setalignment (QT: aligncenter); setcentralwidget (TE); // you can specify the call window 1 qdockwidget * dock = new qdockwidget (TR ("dockwindow1"), this ); dock-> setfeatures (qdockwidget: dockwi Dgetmovable); dock-> setallowedareas (QT: leftdockwidgetarea | QT: rightdockwidgetarea); qtextedit * TE1 = new qtextedit (); TE1-> settext (TR ("window1, the dock widget can be moved between docks by the user "); dock-> setwidget (TE1); adddockwidget (QT: rightdockwidgetarea, Dock ); // set the dock window 2 Dock = new qdockwidget (TR ("dockwindow2"), this); dock-> setfeatures (qdockwidget: dockwidgetclosable | qdockwidget: dockwidgetflo Atable); qtextedit * TE2 = new qtextedit (); TE2-> settext (TR ("window2, the dock widget can floated and can be closed ")); dock-> setwidget (TE2); adddockwidget (QT: rightdockwidgetarea, Dock); // set the dock window 3 dock = new qdockwidget (TR ("dockwindow3"), this ); dock-> setfeatures (qdockwidget: alldockwidgetfeatures); qtextedit * TE3 = new qtextedit (); TE3-> settext (TR ("window3, the dock widget can be closed, moved and floated" ); Dock-> setwidget (TE3); adddockwidget (QT: rightdockwidgetarea, Dock);} mainwindow ::~ Mainwindow (){}
Resolution:
1. Create a docking form for the qdockwidget object:
QDockWidget *dock=new QDockWidget(tr("DockWindow1"),this);
The first parameter is the name of the dock form, and the second parameter is the parent form of the dock form.
2. Set the properties of the dock form:
dock->setFeatures(QDockWidget::DockWidgetMovable);
Set the dock form feature:
× Close: There is a close button
* Movable:
Move the dockwindow to the left.
* Float:
Dockwindow3 can be moved out of the main window.
× Other parameters:
Qdockwidget: alldockwidgetfeatures // all attributes are available.Qdockwidget: nodockwidgetfeatures // The widget cannot be moved, disabled, or float.
3. Add textedit to the dock form
dock->setWidget(te1);
4. Add the docked form to the main form
addDockWidget(Qt::RightDockWidgetArea,dock);
Finally, compile and run: