Qt's docked window QDockWidget class

Posted by Mordred on Thu, 02 Jan 2020 05:06:37 +0100

Qt's docked window QDockWidget class
General process for setting up docked windows
1. Create a docking window for the QDockWidget object
2. To set the properties of this docked form, you usually call setFeatures() and setAllowAreas()
3. Create a new control to insert a docked window. The commonly used controls are QListWidget and QTextEdit
4. Insert the control into the docked window and call the setWidget() method of QDockWidget
5. Use addDockWidget method to add this docked window to MainWindow

Steps:
1. New Qt GUI application, project name DockWindows, base class "QMainWindow", class name "DockWindows", cancel "create interface"
2. Modify the file DockWindows.h and DockWindows.cpp to initialize the window and realize the function in the constructor

DockWindows.h

#ifndef DOCKWINDOWS_H
#define DOCKWINDOWS_H

#include <QMainWindow>


class DockWindows : public QMainWindow
{
    Q_OBJECT

public:
    explicit DockWindows(QWidget *parent = 0);
    ~DockWindows();

};

#endif // DOCKWINDOWS_H

 

DockWindows.cpp

#include "dockwindows.h"
#include "ui_dockwindows.h"
#include<QTextEdit>
#include<QDockWidget>

DockWindows::DockWindows(QWidget *parent) :
    QMainWindow(parent)
{
    setWindowTitle(tr("windows"));  //Set the title block text of the main window
    QTextEdit *te = new QTextEdit(this);  //Define a QTextEdit object as the main window
    te->setText(tr("Main Window"));
    te->setAlignment(Qt::AlignCenter);
    setCentralWidget(te);     //Make this edit box the central form of the main window
    //Docking window 1
    QDockWidget *dock = new QDockWidget(tr("DockWindow1"), this);
    dock->setFeatures(QDockWidget::DockWidgetMovable);  //portability
    dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    //You can dock on the left / right side of the main window
    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);

    //Docking window 2
    dock=new QDockWidget(tr("DockWindow2"),this);
    dock->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetFloatable); //Can be closed and floating
    //  Qdockwidget:: nodockwidget features / / cannot be closed, moved or floated
    dock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
    //Top dock, bottom dock
    QTextEdit *te2 =new QTextEdit();
    te2->setText(tr("Window2,The dock widget can be detached from the main window,""and floated as an independent window, and can be closed"));
    dock->setWidget(te2);
    addDockWidget(Qt::RightDockWidgetArea,dock);

      //Docking window 3
      dock=new QDockWidget(tr("DockWindow3"),this);
      dock->setFeatures(QDockWidget::AllDockWidgetFeatures);     //All characteristics
      QTextEdit *te3 =new QTextEdit();
      te3->setText(tr("Window3,The dock widget can be closed, moved, and floated"));
      dock->setWidget(te3);
      addDockWidget(Qt::RightDockWidgetArea,dock);
}

DockWindows::~DockWindows()
{

}

 

---

Topics: Qt Windows