Demo的效果
头文件中的变量声明
//退出动作QAction* exit;//菜单栏菜单QMenu* filemenu;QMenu* actiona;//在状态栏的标签控件QLabel* label;//两个停靠窗口QDockWidget *dockwidget;QDockWidget *dockwidget_textbox;
CPP源文件中的对象定义
//创建+初始化按钮,将要放到第一个窗口内QPushButton *button=new QPushButton(tr("Exit"),this);QObject::connect(button,SIGNAL(clicked()),this,SLOT(close()));//创建+初始化,放到第二个窗口QTextEdit * textbox=new QTextEdit;//创建两个停靠窗口对象,停靠在this窗口dockwidget=new QDockWidget(this);dockwidget_textbox=new QDockWidget(this);
设置停靠窗口的停靠边允许,内部空间和窗口标题
//允许停靠到四个边的dockwidget停靠窗口dockwidget->setWindowTitle(tr("dockwidget_button"));dockwidget->setAllowedAreas(Qt::AllDockWidgetAreas);dockwidget->setWidget(button);//允许停靠到左和下边的停靠窗口dockwidget_textbox->setWindowTitle(tr("dockwidget_textbox"));dockwidget_textbox->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::BottomDockWidgetArea);dockwidget_textbox->setWidget(textbox);
初始化时窗口的配置和动作对其实现显示与隐藏
//初始化把停靠窗口显示到左边和底边上this->addDockWidget(Qt::LeftDockWidgetArea,dockwidget);this->addDockWidget(Qt::BottomDockWidgetArea,dockwidget_textbox);//设置菜单栏动作使其控制停靠窗口的显示与隐藏this->actiona->addAction(dockwidget_textbox->toggleViewAction());this->actiona->addAction(dockwidget->toggleViewAction());
demo的源码:http://pan.baidu.com/s/1jGAm1T0