Qt learning-docked form qdockwidget class

Source: Internet
Author: User

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:



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.