#include "mainwindow.h"
#include "ui_mainwindow.h"
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");
}
if("widget2" == widget->objectName())
{
Widget2 = widget;
Widget2->setParent(NULL);
Widget2->setWindowFlags(Qt::Dialog| Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
Widget2->setGeometry(mouseEvent->globalX(),mouseEvent->globalY(),300,300);
Widget2->show();
Widget2->move(mouseEvent->globalX(),mouseEvent->globalY());
Widget2->setWindowTitle("Widget2");
}
if("widget3" == widget->objectName())
{
Widget3 = widget;
Widget3->setParent(NULL);
Widget3->setWindowFlags(Qt::Dialog| Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
Widget3->setGeometry(mouseEvent->globalX(),mouseEvent->globalY(),300,300);
Widget3->show();
Widget3->move(mouseEvent->globalX(),mouseEvent->globalY());
Widget3->setWindowTitle("Widget3");
}
if("widget4" == widget->objectName())
{
Widget4 = widget;
Widget4->setParent(NULL);
Widget4->setWindowFlags(Qt::Dialog| Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
Widget4->setGeometry(mouseEvent->globalX(),mouseEvent->globalY(),300,300);
Widget4->show();
Widget4->move(mouseEvent->globalX(),mouseEvent->globalY());
Widget4->setWindowTitle("Widget4");
}
if("widget5" == widget->objectName())
{
Widget5 = widget;
Widget5->setParent(NULL);
Widget5->setWindowFlags(Qt::Dialog| Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
Widget5->setGeometry(mouseEvent->globalX(),mouseEvent->globalY(),300,300);
Widget5->show();
Widget5->move(mouseEvent->globalX(),mouseEvent->globalY());
Widget5->setWindowTitle("Widget5");
}
if("widget6" == widget->objectName())
{
Widget6 = widget;
Widget6->setParent(NULL);
Widget6->setWindowFlags(Qt::Dialog| Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
Widget6->setGeometry(mouseEvent->globalX(),mouseEvent->globalY(),300,300);
Widget6->show();
Widget6->move(mouseEvent->globalX(),mouseEvent->globalY());
Widget6->setWindowTitle("Widget6");
}
}
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);
}
if(("widget2" == widget->objectName()) && (NULL != ui->widget2_back->layout()))
{
Layout2 = ui->widget2_back->layout();
while(ui->widget2_back->layout()->count() > 0)
{
QLayoutItem *item = ui->widget2_back->layout()->takeAt(0);
delete item;
}
ui->widget2_back->setMaximumWidth(0);
}
if(("widget3" == widget->objectName()) && (NULL != ui->widget3_back->layout()))
{
Layout3 = ui->widget3_back->layout();
while(ui->widget3_back->layout()->count() > 0)
{
QLayoutItem *item = ui->widget3_back->layout()->takeAt(0);
delete item;
}
ui->widget3_back->setMaximumWidth(0);
}
if(("widget4" == widget->objectName()) && (NULL != ui->widget4_back->layout()))
{
Layout4 = ui->widget4_back->layout();
while(ui->widget4_back->layout()->count() > 0)
{
QLayoutItem *item = ui->widget4_back->layout()->takeAt(0);
delete item;
}
ui->widget4_back->setMaximumWidth(0);
}
if(("widget5" == widget->objectName()) && (NULL != ui->widget5_back->layout()))
{
Layout5 = ui->widget5_back->layout();
while(ui->widget5_back->layout()->count() > 0)
{
QLayoutItem *item = ui->widget5_back->layout()->takeAt(0);
delete item;
}
ui->widget5_back->setMaximumWidth(0);
}
if(("widget6" == widget->objectName()) && (NULL != ui->widget6_back->layout()))
{
Layout6 = ui->widget6_back->layout();
while(ui->widget6_back->layout()->count() > 0)
{
QLayoutItem *item = ui->widget6_back->layout()->takeAt(0);
delete item;
}
ui->widget6_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);
}el