Qt学习02:停靠窗口DDockWidget类

时间:2021-06-10 20:01:03

/*dockwindows.cpp*/

#include "dockwindows.h"
#include <QMainWindow>
#include <QTextEdit>
#include <QDockWidget>
 
DockWindows::DockWindows(QWidget *parent) : QMainWindow(parent)
{
    setWindowTitle("Dockwindows");
    QTextEdit *main = new QTextEdit(this);
    main->setText(tr("main widnow"));
    main->setAlignment(Qt::AlignCenter);
    setCentralWidget(main);
    //first DockWidget
    QDockWidget *dock1 = new QDockWidget(tr("dock1 window"),this);
    dock1->setFeatures(QDockWidget::DockWidgetMovable);
    dock1->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
    //
    QTextEdit *dockEditText = new QTextEdit();
    dockEditText->setText(tr("first dock window"));
    dock1->setWidget(dockEditText);
    addDockWidget(Qt::RightDockWidgetArea,dock1);//右侧放置
    //second DockWidget
    QDockWidget *dock2 = new QDockWidget(tr("dock2 widown"),this);
    dock2->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetFloatable);
    QTextEdit *dockEditText2 = new QTextEdit();
    dockEditText2->setText("second dock windw");
    dock2->setWidget(dockEditText2);
    addDockWidget(Qt::RightDockWidgetArea,dock2);//右侧放置
    //third DockWidget
    QDockWidget *dock3 = new QDockWidget(tr("dock3 widown"),this);
    dock3->setFeatures(QDockWidget::AllDockWidgetFeatures);
    QTextEdit *dockEditText3 = new QTextEdit();
    dockEditText3->setText("third dock windw");
    dock3->setWidget(dockEditText3);
    addDockWidget(Qt::RightDockWidgetArea,dock3); //右侧放置
}
 

/*dockwindows.h*/

#ifndef DOCKWINDOWS_H
#define DOCKWINDOWS_H
#include <QMainWindow>
 
class DockWindows : public QMainWindow
{
    Q_OBJECT
public:
    explicit DockWindows(QWidget *parent = 0);
 
signals:
 
public slots:
};
 
#endif // DOCKWINDOWS_H