2016-04-01 92 views
4

我有一個應用程序,有3個主要的部件。我也有一個彈出式的QDockWidget。我試圖讓QDockWidget停靠在底部窗口小部件的右半部分,但正如您在下圖中看到的那樣,我可以停靠窗口的唯一位置位於應用程序的邊緣。我怎樣才能使QDockWidget窗口占據底部小部件的右半部分?如何停靠彈出窗口?

enter image description here

此外,有沒有辦法有一個QDockWidget是已經停靠在打開的應用程序,而不必在其自己的窗口單獨打開?

編輯:使用@下面Bertrand的答案,這裏是我結束了做:

mainwindow.h

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

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


private: 
    Ui::MainWindow *ui; 
    void on_actionRestore_layout_triggered(); 
    QMainWindow* m_rightSideWindow; 
    QDockWidget* m_dockWidget1; 
    QDockWidget* m_dockWidget2; 
    QDockWidget* m_dockWidget3; 
}; 

#endif // MAINWINDOW_H 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

#include <QtWidgets> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow), 
    m_rightSideWindow(NULL), 
    m_dockWidget1(NULL), 
    m_dockWidget2(NULL), 
    m_dockWidget3(NULL) 
{ 
    ui->setupUi(this); 
    QSplitter *splitter = new QSplitter(this); 
     splitter->setOrientation(Qt::Horizontal); 
     QTreeView* leftSideWidget = new QTreeView(this); 

     m_rightSideWindow = new QMainWindow(this); 
     m_rightSideWindow->setWindowFlags(Qt::Widget); 
     m_rightSideWindow->layout()->setContentsMargins(3, 3, 3, 3); 

     splitter->addWidget(leftSideWidget); 
     splitter->addWidget(m_rightSideWindow); 

     m_dockWidget1 = new QDockWidget("Dock 1", this); 
     m_rightSideWindow->addDockWidget(Qt::TopDockWidgetArea, m_dockWidget1); 
     m_dockWidget1->setTitleBarWidget(new QWidget()); // remove title bar 
     m_dockWidget1->setAllowedAreas(Qt::NoDockWidgetArea); // do not allow to dock 
     QTextEdit* textEdit1 = new QTextEdit(this); // put any QWidget derived class inside 
     m_dockWidget1->setWidget(textEdit1); 

     m_dockWidget2 = new QDockWidget("Dock 2", this); 
     m_rightSideWindow->addDockWidget(Qt::BottomDockWidgetArea, m_dockWidget2); 
     m_dockWidget2->setTitleBarWidget(new QWidget()); 
     m_dockWidget2->setAllowedAreas(Qt::NoDockWidgetArea); 
     QTextEdit* textEdit2 = new QTextEdit(this); 
     m_dockWidget2->setWidget(textEdit2); 

     m_dockWidget3 = new QDockWidget("Dock 3", this); 
     m_rightSideWindow->addDockWidget(Qt::BottomDockWidgetArea, m_dockWidget3); 
     QTextEdit* textEdit3 = new QTextEdit(this); 
     m_dockWidget3->setWidget(textEdit3); 

     setCentralWidget(splitter); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::on_actionRestore_layout_triggered() 
{ 
    QList<QDockWidget*> list = findChildren<QDockWidget*>(); 
    foreach(QDockWidget* dock, list) 
    { 
     if(dock->isFloating()) 
      dock->setFloating(false); 
     m_rightSideWindow->removeDockWidget(dock); 
     if (dock == m_dockWidget1) 
      m_rightSideWindow->addDockWidget(Qt::TopDockWidgetArea, m_dockWidget1); 
     else 
      m_rightSideWindow->addDockWidget(Qt::BottomDockWidgetArea, dock); 
     dock->setVisible(true); 
    } 
    m_rightSideWindow->splitDockWidget(m_dockWidget2, m_dockWidget3, Qt::Horizontal); 
} 

回答

6

你可以將QDockWidget停靠在QMainWindow或其他Q上DockWidget。

,以獲得所需的佈局嵌入您的主窗口的右側子的QMainWindow,並把它作爲與setWindowFlags一個QWidget(QT ::小部件):

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

#include <QtWidgets> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    QSplitter *splitter = new QSplitter(this); 
    splitter->setOrientation(Qt::Horizontal); 
    QTreeView* leftSideWidget = new QTreeView(this); 

    m_rightSideWindow = new QMainWindow(this); 
    m_rightSideWindow->setWindowFlags(Qt::Widget); 
    m_rightSideWindow->layout()->setContentsMargins(3, 3, 3, 3); 

    splitter->addWidget(leftSideWidget); 
    splitter->addWidget(m_rightSideWindow); 

    m_dockWidget1 = new QDockWidget("Dock 1", this); 
    m_rightSideWindow->addDockWidget(Qt::TopDockWidgetArea, m_dockWidget1); 
    m_dockWidget1->setTitleBarWidget(new QWidget()); // remove title bar 
    m_dockWidget1->setAllowedAreas(Qt::NoDockWidgetArea); // do not allow to dock 
    QTextEdit* textEdit1 = new QTextEdit(this); // put any QWidget derived class inside 
    m_dockWidget1->setWidget(textEdit1); 

    m_dockWidget2 = new QDockWidget("Dock 2", this); 
    m_rightSideWindow->addDockWidget(Qt::BottomDockWidgetArea, m_dockWidget2); 
    m_dockWidget2->setTitleBarWidget(new QWidget()); 
    m_dockWidget2->setAllowedAreas(Qt::NoDockWidgetArea); 
    QTextEdit* textEdit2 = new QTextEdit(this); 
    m_dockWidget2->setWidget(textEdit2); 

    m_dockWidget3 = new QDockWidget("Dock 3", this); 
    m_rightSideWindow->addDockWidget(Qt::BottomDockWidgetArea, m_dockWidget3); 
    QTextEdit* textEdit3 = new QTextEdit(this); 
    m_dockWidget3->setWidget(textEdit3); 

    setCentralWidget(splitter); 

} 
MainWindow::~MainWindow() 
{ 
    delete ui; 
} 
void MainWindow::on_actionRestore_layout_triggered() 
{ 
    QList<QDockWidget*> list = findChildren<QDockWidget*>(); 
    foreach(QDockWidget* dock, list) 
    { 
     if(dock->isFloating()) 
      dock->setFloating(false); 
     m_rightSideWindow->removeDockWidget(dock); 
     if (dock == m_dockWidget1) 
      m_rightSideWindow->addDockWidget(Qt::TopDockWidgetArea, m_dockWidget1); 
     else 
      m_rightSideWindow->addDockWidget(Qt::BottomDockWidgetArea, dock); 
     dock->setVisible(true); 
    } 
    m_rightSideWindow->splitDockWidget(m_dockWidget2, m_dockWidget3, Qt::Horizontal); 
} 

enter image description here

+0

這是一個美妙的答案。感謝您花時間寫出來。一個問題:'m_rightSideWindow'應聲明爲什麼類型('error:'m_rightSideWindow'未在此範圍內聲明')? – orbit

+0

沒關係。我想到了。查看我的原始答案的編輯部分了解詳細信息。 – orbit

+1

@orbit對不起,我還沒有給#include文件。你做到了! :-) – Bertrand