#include "qminmaxwindowwidget.h" #include "qevent.h" #include "qtimer.h" QMinMaxWindowWidget::QMinMaxWindowWidget(QWidget *parent) : QWidget(parent), m_contrl(nullptr) { ui.setupUi(this); setAttribute(Qt::WA_DeleteOnClose, true); m_contentMargin = QMargins(3, 3, 3, 3); connect(ui.toolButtonClose, SIGNAL(clicked()), this, SLOT(closeWindow())); connect(ui.toolButtonMax, SIGNAL(clicked()), this, SLOT(switchWindow())); connect(ui.toolButtonMin, SIGNAL(clicked()), this, SLOT(minimizedWindow())); } QMinMaxWindowWidget::~QMinMaxWindowWidget() { } void QMinMaxWindowWidget::setControlWidget(QWidget * contrl) { m_contrl = contrl; if (m_contrl)m_contrl->installEventFilter(this); initalizeIcons(); } bool QMinMaxWindowWidget::eventFilter(QObject * o, QEvent * e) { if (o == m_contrl){ if (e->type() == QEvent::Show){ QShowEvent * event = (QShowEvent *)e; QTimer::singleShot(100, this, &QMinMaxWindowWidget::initalizeIcons); } } return false; } void QMinMaxWindowWidget::initalizeIcons() { if (m_contrl&&m_contrl->isMaximized()){ if (m_contrl->layout()) { m_contentMargin = m_contrl->layout()->contentsMargins(); m_contrl->layout()->setContentsMargins(QMargins(0, 0, 0, 0)); } } } void QMinMaxWindowWidget::switchWindow() { if (m_contrl){ if (m_contrl->isMaximized()){ m_contrl->showNormal(); if (m_contrl->layout()) { m_contrl->layout()->setContentsMargins(QMargins(3, 3, 3, 3)); } ui.toolButtonMax->setToolTip(QString::fromStdWString(L"最大化")); } else{ if (m_contrl->layout()) { m_contrl->layout()->setContentsMargins(QMargins(0, 0, 0, 0)); } m_contrl->showMaximized(); ui.toolButtonMax->setToolTip(QString::fromStdWString(L"还原")); //QIcon icon; //icon.addFile(QString::fromLocal8Bit(":/conctrl/Resources/0huanyuan.png")); //ui.toolButtonMax->setIcon(icon); } } } void QMinMaxWindowWidget::minimizedWindow() { if (m_contrl)m_contrl->showMinimized(); } void QMinMaxWindowWidget::closeWindow() { if (m_contrl){ m_contrl->close(); } } void QMinMaxWindowWidget::setMaxVisible(bool visible) { ui.toolButtonMax->setVisible(visible); } void QMinMaxWindowWidget::setMinVisible(bool visible) { ui.toolButtonMin->setVisible(visible); } void QMinMaxWindowWidget::setCloseVisible(bool visible) { ui.toolButtonClose->setVisible(visible); }