用以实现一个停靠窗口的功能
1.新建一个无ui界面的工程,基类为widget或者mainwindow(我这里为mainwindow)
2.mainwindow.h和mainwindow.cpp中添加相关头文件
mainwindow.h
#include
<QTextEdit>
#include
<QToolBar>
#include
<QMenuBar>
####
mainwindow.cpp
#include
<QDockWidget>
#include
<QCalendarWidget>
添加一个日历控件作为可拖拽的基础
3.代码实现
mainwindow.h
#include
<QMainWindow>
#include
<QTextEdit>
#include
<QToolBar>
#include
<QMenuBar>
class MainWindow : public QMainWindow
{
Q_OBJECT
QTextEdit
* textedit
;
QToolBar
* viewtoolbar
;
QMenu
* viewmenu
;
public:
void creatDockWidget();
public:
MainWindow(QWidget
*parent
= 0);
~MainWindow();
};
mainwindow.cpp
#include
"mainwindow.h"
#include
<QDockWidget>
#include
<QCalendarWidget>
MainWindow
::MainWindow(QWidget
*parent
)
: QMainWindow(parent
)
{
textedit
= new QTextEdit ;
this->setCentralWidget(textedit
);
viewmenu
= menuBar()->addMenu(tr("视图"));
viewtoolbar
= this->addToolBar(tr("日历"));
creatDockWidget();
this->setWindowTitle("停靠窗口");
}
MainWindow
::~MainWindow()
{
}
void MainWindow
::creatDockWidget()
{
QDockWidget
* dock
= new QDockWidget(this);
dock
->setAllowedAreas(Qt
::AllDockWidgetAreas
);
QCalendarWidget
* calendar
= new QCalendarWidget;
dock
->setWidget(calendar
);
this->addDockWidget(Qt
::RightDockWidgetArea
,dock
);
dock
->setFeatures(QDockWidget
::DockWidgetMovable
| QDockWidget
::DockWidgetClosable
);
viewmenu
->addAction(dock
->toggleViewAction());
viewtoolbar
->addAction(dock
->toggleViewAction());
}
4.效果展示
转载请注明原文地址: https://win8.8miu.com/read-1625.html