QT5 docking window with various controls

Posted by ace21 on Fri, 01 Nov 2019 13:54:18 +0100

Docking window can be a convenient and fast management interface, which can make users feel the convenient operation of the application. But the docked window itself is a badge, and it can't add and arrange the controls on its own. This paper provides a simple method to add controls to docked windows.

1. First, let's talk about the QDockWidge class of docked window.

#include <QTextEdit>
#include <QDockWidget>
//CPP document part reference code
setWindowTitle(tr("V1.0"));//Form title
QTextEdit *t1 = new QTextEdit(this);
t1->setText(tr("Main Window"));
t1->setAlignment(Qt::AlignCenter);      //Seamless splicing of several qlabels
//main window
setCentralWidget(t1);
//docked window
QDockWidget *dock1 = new QDockWidget(tr("DockWindow1"));
dock1->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable); //Windows can be moved
dock1->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea,dock1);

After the DockWindow is established, it needs to be set. There are two parameters to set.
1.1 QDockWidget::DockWidgetFeatures

Parameter DockWidgetFeatures Characteristic
QDockWidget::DockWidgetClosable Dock window to close
QDockWidget::DockWidgetMovable Docked windows can be moved
QDockWidget::DockWidgetFloatable Docked windows can float
QDockWidget::DockWidgetClosable All three are acceptable.
QDockWidget::DockWidgetClosable None of them

1.2QDockWidget::DockWidgetAreas

Parameter QDockWidget::DockWidgetAreas Characteristic
Qt::LeftDockWidgetArea Left side dockable
Qt::RightDockWidgetArea Right side dockable
Qt::TopDockWidgetArea Top side dockable
Qt::BottomDockWidgetArea Bottom side dockable
Qt::AllDockWidgetArea All four are acceptable.
Qt::NoDockWidgetArea None of the four

If more than one feature is needed, it can be implemented in the way of or (|).

2. Next, how to insert the control freely
2.1 the following controls are initialized, no need to repeat.

  InitELabel = new QLabel(tr("Init E(V): "));
    InitELineEdit = new QLineEdit;
    FinalELabel = new QLabel(tr("Final E(V): "));
    FinalELineEdit = new QLineEdit;
    ScanRateLabel = new QLabel(tr("Scan Rate (mV/s): "));
    ScanRateLineEdit = new QLineEdit;
    ScanningDirectionLabel = new QLabel(tr("Scanning direction: "));
    ScanningDirectionComboBox = new QComboBox;
    ScanningDirectionComboBox->addItem(tr("Positive"));
    ScanningDirectionComboBox->addItem(tr("Negative"));
    GainLabel = new QLabel(tr("Gain"));
    GainLineEdit = new QLineEdit;
    SampleIntervalLabel = new QLabel(tr("Sample Interval (mv): "));
    SampleIntervalLineEdit = new QLineEdit;
    QuietTimeLabel = new QLabel(tr("Sample Interval (mv): "));
    QuietTimeLineEdit = new QLineEdit;
    StartBtn = new QPushButton(tr("Start"));
    PauseBtn = new QPushButton(tr("Pause"));
    StopBtn = new QPushButton(tr("Stop"));

2.2 create a new QGridLayout layout and insert each control

QGridLayout *Dock2Layout = new QGridLayout();
    Dock2Layout->setAlignment(Qt::AlignCenter);
    Dock2Layout->addWidget(InitELabel,0,0);
    Dock2Layout->addWidget(InitELineEdit,0,1);
    Dock2Layout->addWidget(FinalELabel,1,0);
    Dock2Layout->addWidget(FinalELineEdit,1,1);
    Dock2Layout->addWidget(ScanRateLabel,2,0);
    Dock2Layout->addWidget(ScanRateLabel,2,1);
    Dock2Layout->addWidget(ScanningDirectionLabel,3,0);
    Dock2Layout->addWidget(ScanningDirectionComboBox,3,1);
    Dock2Layout->addWidget(GainLabel,4,0);
    Dock2Layout->addWidget(GainLineEdit,4,1);
    Dock2Layout->addWidget(SampleIntervalLabel,5,0);
    Dock2Layout->addWidget(SampleIntervalLineEdit,5,1);
    Dock2Layout->addWidget(QuietTimeLabel,6,0);
    Dock2Layout->addWidget(QuietTimeLineEdit,6,1);
    Dock2Layout->addWidget(StartBtn,7,0);
    Dock2Layout->addWidget(PauseBtn,7,1);
    Dock2Layout->addWidget(StopBtn,7,2);

Through this layout, we can arrange the position of each control independently, which is very convenient.

2.3 create a new QWidget and insert the layout as a Widget
QWidget *Dock2Widget = new QWidget();
Dock2Widget->setLayout(Dock2Layout);
dock2->setWidget(Dock2Widget);


If you have any questions, please leave a message. It's better to pay attention.~

Topics: Qt Windows