环境
Qt 5.4.2
功能
鼠标拖拽widget,分离出新的窗口,鼠标关闭窗口后,widget恢复之前的位置
源码
1.mainWindow.h
class MainWindow : public QMainWindow
{
private:
QWidget *ObjWidget;
QWidget *Widget1;
QWidget *Widget2;
QWidget *Widget3;
QWidget *Widget4;
QWidget *Widget5;
QWidget *Widget6;
QLayout *Layout1;
QLayout *Layout2;
QLayout *Layout3;
QLayout *Layout4;
QLayout *Layout5;
QLayout *Layout6;
private slots:
bool eventFilter(QObject *object, QEvent *event);
public:
void MovePosWidget(QWidget *widget, QEvent *event);
void RecoverWidget(QWidget *widget);
void DeleteLayout(QWidget *widget);
void DisplayWidget(QWidget *widget, QEvent *event);
};
2.mainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widget1->installEventFilter(this);
ui->widget2->installEventFilter(this);
ui->widget3->installEventFilter(this);
ui->widget4->installEventFilter(this);
ui->widget5->installEventFilter(this);
ui->widget6->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
static QPoint lastPoint;
static bool isHover = false;
static bool isHiden = false;
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
ObjWidget = NULL;
ObjWidget = qobject_cast<QWidget*>(object);
//筛选出Widget
if(ObjWidget == NULL) { return QMainWindow::eventFilter(object,event); }
switch(event->type())
{
//鼠标按下事件
case QEvent::MouseButtonPress:{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if(ObjWidget->rect().contains(mouseEvent->pos()) && (mouseEvent->button() == Qt::LeftButton))
{
lastPoint = mouseEvent->pos();
isHover = true;
}
}break;
//鼠标移动
case QEvent::MouseMove:{
if(!isHover) { break; }
if(NULL == ObjWidget->parent())
{
MovePosWidget(ObjWidget,event); //Widget窗口随鼠标移动
return QMainWindow::eventFilter(ObjWidget,event);
}
DeleteLayout(ObjWidget); //删除布局
DisplayWidget(ObjWidget,event); //显示窗口
}break;
//鼠标释放
case QEvent::MouseButtonRelease:{
if(!isHover) { break; }
if(NULL == ObjWidget->parent()) { return QMainWindow::eventFilter(ObjWidget,event); }
isHover = false;
}break;
case QEvent::Close:{
RecoverWidget(ObjWidget); //恢复显示
isHiden = true;
}break;
case QEvent::Hide:{
if(isHiden)
{
isHiden = false;
}else{
return QMainWindow::eventFilter(ObjWidget,event);
}
}break;
default:break;
}
return false;
}
void MainWindow::DisplayWidget(QWidget *widget,QEvent *event)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if("widget1" == widget->objectName())
{
Widget1 = widget;
Widget1->setParent(NULL);
Widget1->setWindowFlags(Qt::Dialog| Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
Widget1->setGeometry(mouseEvent->globalX(),mouseEvent->globalY(),300,300);
Widget1->show();
Widget1->move(mouseEvent->globalX(),mouseEvent->globalY());
Widget1->setWindowTitle("Widget1");
//......
}
}
void MainWindow::DeleteLayout(QWidget *widget)
{
if(("widget1" == widget->objectName()) && (NULL != ui->widget1_back->layout()))
{
Layout1 = ui->widget1_back->layout();
while(ui->widget1_back->layout()->count() > 0)
{
QLayoutItem *item = ui->widget1_back->layout()->takeAt(0);
delete item;
}
ui->widget1_back->setMaximumWidth(0);
}
//......
}
void MainWindow::RecoverWidget(QWidget *widget)
{
if("widget1" == widget->objectName())
{
Widget1->setParent(ui->widget1_back);
Layout1->removeWidget(Widget1);
Layout1->addWidget(Widget1);
Layout1->setMargin(0);
ui->widget1_back->setLayout(Layout1);
ui->widget1_back->setMaximumWidth(16777215);
}
//......
}
void MainWindow::MovePosWidget(QWidget *widget, QEvent *event)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if("widget1" == widget->objectName())
{
widget->move(mouseEvent->globalX(),mouseEvent->globalY());
}
//......
}
运行