下面是这个例子的效果:
当然还可以控制两个停靠窗口停在四个停靠区的哪个区,这里不贴效果图了。
代码如下:
//dockwindow.h
#ifndef DOCKWINDOW_H
#define DOCKWINDOW_H
#include <QMainWindow>
class QMenu;
class QToolBar;
class QAction;
class QTextEdit;
class DockWindow : public QMainWindow
{
Q_OBJECT
public:
DockWindow(QWidget *parent = 0);
~DockWindow();
private slots:
void newFile();
void save();
void about();
private:
void createMenus();
void createActions();
void createToolBars();
void createStatusBars();
void createDockWindows();
QMenu *fileMenu;
QMenu *viewMenu;
QMenu *helpMenu;
QToolBar *fileToolBar;
QToolBar *viewToolBar;
QToolBar *helpToolBar;
QAction *newAction;
QAction *saveAction;
QAction *exitAction;
QAction *aboutAction;
QTextEdit *textEdit;
};
#endif // DOCKWINDOW_H
//dockwindow.cpp
#include <QtGui>
#include "dockwindow.h"
DockWindow::DockWindow(QWidget *parent)
: QMainWindow(parent)
{
textEdit = new QTextEdit;
setCentralWidget(textEdit);
createActions();
createMenus();
createToolBars();
createStatusBars();
createDockWindows();
setWindowTitle(tr("Dock Window"));
}
void DockWindow::newFile()
{
textEdit->clear();
QMessageBox::about(this, tr("Dock Window"),
tr("you triggered the"
"\nnewFileSlot successfully"));
}
void DockWindow::save()
{
QMessageBox::information(this, tr("Dock Window"),
tr("Save the file ?"),
QMessageBox::Ok | QMessageBox::No
| QMessageBox::Cancel);
}
void DockWindow::about()
{
QMessageBox::warning(this, tr("Dock Window"),
tr("Thanks"),QMessageBox::Yes | QMessageBox::No);
}
void DockWindow::createActions()
{
newAction = new QAction(tr("&New"),this);
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("create a new file"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
saveAction = new QAction(tr("&Save"),this);
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("save a exist file"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
exitAction = new QAction(tr("E&xit"),this);
exitAction->setShortcut(QKeySequence::Close);
exitAction->setStatusTip(tr("quit the application"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
aboutAction = new QAction(tr("About"),this);
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
}
void DockWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(saveAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
viewMenu = menuBar()->addMenu(tr("&View"));
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
}
void DockWindow::createToolBars()
{
fileToolBar = addToolBar(tr("&File"));
fileToolBar->addAction(newAction);
fileToolBar->addAction(saveAction);
fileToolBar->addAction(exitAction);
viewToolBar = addToolBar(tr("&View"));
helpToolBar = addToolBar(tr("&Help"));
helpToolBar->addAction(aboutAction);
}
void DockWindow::createStatusBars()
{
statusBar()->showMessage(tr("Ready"));
}
void DockWindow::createDockWindows()
{
QCalendarWidget *calendar = new QCalendarWidget;
QDockWidget *calendarDockWidget = new QDockWidget(this);
calendarDockWidget->setWindowTitle(tr("Calendar"));
calendarDockWidget->setWidget(calendar);
calendarDockWidget->setFeatures(QDockWidget::AllDockWidgetFeatures);
calendarDockWidget->setAllowedAreas(Qt::AllDockWidgetAreas);
addDockWidget(Qt::RightDockWidgetArea,calendarDockWidget);
viewMenu->addAction(calendarDockWidget->toggleViewAction());
viewToolBar->addAction(calendarDockWidget->toggleViewAction());
QDockWidget *textDockWidget = new QDockWidget(this);
textDockWidget->setWindowTitle(tr("About Me"));
textDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::BottomDockWidgetArea);
QTextEdit *myInfo = new QTextEdit(tr("<br>Welcom</br><br>blog address:hhtp://blog.csdn.net/qiurisuixiang</br>"));
textDockWidget->setWidget(myInfo);
addDockWidget(Qt::BottomDockWidgetArea, textDockWidget);
viewMenu->addAction(textDockWidget->toggleViewAction());
viewToolBar->addAction(textDockWidget->toggleViewAction());
}
DockWindow::~DockWindow()
{
}
//main.cpp
#include "dockwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DockWindow dockWindow;
dockWindow.show();
return app.exec();
}
总结一下,创建一个停靠窗口大概需要以下几个步骤:
1、创建一个自己的widget窗口,并设置它的各种属性(这里创建了一个QCalendarWidget和一个QTextEdit对象,并对其一些属性进行了设置)。
2、创建一个QDockWidget对象,并设置这个QDockWidget对象的一些属性。
诸如: calendarDockWidget->setFeatures(QDockWidget::AllDockWidgetFeatures);
calendarDockWidget->setAllowedAreas(Qt::AllDockWidgetAreas);
3、把自己的widget窗口添加到这个停靠窗口中。
calendarDockWidget->setWidget(calendar); (这里说添加可能不太准确,一时找不懂合适的词来描述)。
4、把这个停靠窗口加到QMainWindow的指定的位置。
addDockWidget(Qt::RightDockWidgetArea,calendarDockWidget);