完全解决最大化最小化问题

master
DESKTOP-4RNDQIC\29019 2020-07-05 22:35:35 +08:00
parent abc1cf3110
commit 400e0453c7
5 changed files with 152 additions and 87 deletions

View File

@ -26,7 +26,8 @@
#define QSSDIALOG_SHADOW_WIDTH 12 //QFrame#dialog,QFrame#messagebox padding
#define QSSDIALOG_BODER_WIDTH 0
QssTtitleBar::QssTtitleBar(QWidget *parent , QTitleBar_Type type/* = QTitleBar_Type_Window*/)
QssTtitleBar::QssTtitleBar(QWidget *parent ,
QTitleBar_Type type/* = QTitleBar_Type_Window*/)
: QWidget(parent),
m_maxOrRestore(false),
m_pressed(false),
@ -88,6 +89,7 @@ QssTtitleBar::QssTtitleBar(QWidget *parent , QTitleBar_Type type/* = QTitleBar_T
setWindowFlags(windowFlags()|
Qt::MSWindowsFixedSizeDialogHint);
this->setGeometry(parent->geometry().x(),parent->geometry().y(),0,0);
m_rcNormal = parentWidget()->geometry();
}
QssTtitleBar::~QssTtitleBar()
@ -128,9 +130,15 @@ void QssTtitleBar::onMaxOrRestore()
{
if (m_type != QTitleBar_Type_MainWindow)
return ;
emit(OnMaxOrRestore(m_maxOrRestore));
/*
if (m_maxOrRestore)
{
parentWidget()->setMinimumSize(0, 0);
parentWidget()->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
setMinimumSize(0, 0);
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
if (!m_rcNormal.isValid())
{
QSize sizeHint = parentWidget()->sizeHint();
@ -145,32 +153,40 @@ void QssTtitleBar::onMaxOrRestore()
m_rcNormal.setHeight(sizeHint.height());
}
parentWidget()->setGeometry(m_rcNormal);
parentWidget()->setFixedHeight(m_rcNormal.height());
}
else
{
parentWidget()->setMinimumSize(0, 0);
parentWidget()->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
setMinimumSize(0, 0);
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
QSize sizeHint = parentWidget()->sizeHint();
QDesktopWidget desktop;
QRect rc = desktop.availableGeometry(-1);
QRect curRcMain = m_Main->geometry();
QRect curRcFrame = parentWidget()->geometry();
QRect curRcCentral = m_Main->centralWidget()->geometry();
QRect curRcFrame = parentWidget()->geometry();
if (rc.width() < m_rcValid.width()&& rc.height()< m_rcValid.height())
m_rcNormal = curRcFrame;
qDebug()<<"current geometry is "<<curRcMain<<curRcFrame<<curRcCentral;
QRect rcFrame = parentWidget()->geometry();
if (rc.width() < m_rcValid.width() && rc.height() < m_rcValid.height())
m_rcNormal = rcFrame;
qDebug()<<"parent is"<<parentWidget()->objectName()<<m_Main->objectName();
parentWidget()->setGeometry(rc);
this->m_Main->setGeometry(rc);
m_Main->centralWidget()->setGeometry(rc);
rc.setRight(rc.right() - 50);
m_Main->setGeometry(rc);
//m_Main->setFixedHeight(rc.height());
curRcMain = m_Main->geometry();
curRcFrame = parentWidget()->geometry();
curRcCentral = m_Main->centralWidget()->geometry();
m_Main->setFixedHeight(rc.height());
QSizePolicy mainPolicy = m_Main->sizePolicy();
qDebug()<<mainPolicy;
//pmain->centralWidget()->setFixedHeight(rc.height());
qDebug()<<"current geometry is "<<curRcMain<<curRcFrame<<curRcCentral;
}
}*/
setMaxOrRestore(!m_maxOrRestore);
}
void QssTtitleBar::paintEvent(QPaintEvent *)
@ -231,6 +247,63 @@ bool QssTtitleBar::eventFilter( QObject * obj, QEvent * ev )
return QWidget::eventFilter(obj, ev);
}
void QssMainWindow::OnMaxOrRestore(bool max)
{
qDebug()<<"sended";
if (max)
{
setMinimumSize(0, 0);
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
this->m_frame->setMinimumSize(0, 0);
this->m_frame->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
if (!m_rcNormal.isValid())
{
QSize sizeHint = parentWidget()->sizeHint();
if (sizeHint.width() > m_rcValid.width())
sizeHint.setWidth(m_rcValid.width() -100);
if (sizeHint.height() > m_rcValid.height())
sizeHint.setHeight(m_rcValid.height() - 100);
m_rcNormal.setLeft((m_rcValid.width() - sizeHint.width())/2);
m_rcNormal.setTop((m_rcValid.height() - sizeHint.height())/2);
m_rcNormal.setWidth(sizeHint.width());
m_rcNormal.setHeight(sizeHint.height());
}
this->setGeometry(m_rcNormal);
this->m_frame->setGeometry(m_rcNormal);
this->centralWidget()->setGeometry(m_rcNormal);
this->setFixedHeight(m_rcNormal.height() + this->titleBar()->height());
this->m_frame->setFixedHeight(m_rcNormal.height() + this->titleBar()->height());
setMinimumSize(0, 0);
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
this->m_frame->setMinimumSize(0, 0);
this->m_frame->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
}
else
{
setMinimumSize(0, 0);
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
this->m_frame->setMinimumSize(0, 0);
this->m_frame->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
QSize sizeHint = parentWidget()->sizeHint();
QDesktopWidget desktop;
QRect rc = desktop.availableGeometry(-1);
qDebug()<<"parent is"<<parentWidget()->objectName()<<this->objectName();
parentWidget()->setGeometry(rc);
rc.setRight(rc.right() - 50);
this->setGeometry(rc);
this->setFixedHeight(rc.height());
}
}
QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = 0*/)
: QMainWindow(parent, flags),
m_mousePressedInBoundy(false),
@ -242,11 +315,12 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
//css
m_frame->setObjectName("window");
m_frame->setWindowFlags(Qt::Window | Qt::FramelessWindowHint| Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
m_frame->setWindowFlags(Qt::Window |
Qt::FramelessWindowHint|
Qt::WindowSystemMenuHint |
Qt::WindowMinimizeButtonHint);
m_frame->setMouseTracking(true);
m_frame->installEventFilter(this);
m_titleBar = new QssTtitleBar(m_frame);
m_titleBar->installEventFilter(this);
m_titleBar->SetMainWindow(this);
@ -259,12 +333,19 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
vbox->setSpacing(0);
vbox->addStretch();
qDebug()<<this->children()[0]->objectName();
installEventFilter(this);
mFrameRect = m_frame->geometry();
m_rcNormal = this->geometry();
connect(this->titleBar(),SIGNAL( OnMaxOrRestore(bool )),this,SLOT(OnMaxOrRestore(bool)));
}
QssMainWindow::~QssMainWindow()
{
m_frame->deleteLater();
}
void QssMainWindow::show()
{
@ -328,8 +409,6 @@ void QssMainWindow::showFullScreen()
{
m_titleBar->setMaxOrRestore(true);
m_frame->showFullScreen();
this->showFullScreen();
this->centralWidget()->showFullScreen();
}
void QssMainWindow::showNormal()
@ -365,11 +444,6 @@ int QssMainWindow::CalCursorCol(QPoint pt)
void QssMainWindow::onMouseMoveEvent( QMouseEvent * ev )
{
QObjectList t = this->children();
foreach(auto obj,t){
qDebug()<<"object name"<<obj->objectName();
}
if (m_titleBar->maxOrRestore())
{
return;
@ -396,12 +470,12 @@ void QssMainWindow::onMouseMoveEvent( QMouseEvent * ev )
return;
if ((m_top || m_bottom) && qAbs(dy) < 5)
return;
if (m_left && dx > 0 && m_rect.width() <= m_frame->minimumWidth())
if (m_left && dx > 0 && mFrameRect.width() <= m_frame->minimumWidth())
return ;
if (m_top && dy > 0 && m_rect.height() <= m_frame->minimumHeight())
if (m_top && dy > 0 && mFrameRect.height() <= m_frame->minimumHeight())
return;
QRect rc = m_rect;
QRect rc = mFrameRect;
if (m_left){
rc.setLeft(rc.left() + dx);
rtCentralGeo.setLeft(rtCentralGeo.left() + dx);
@ -422,10 +496,8 @@ void QssMainWindow::onMouseMoveEvent( QMouseEvent * ev )
rtCentralGeo.setBottom(rtCentralGeo.bottom() + dy);
rtMainWindow.setBottom(rtMainWindow.bottom() + dy);
}
QObjectList t = m_frame->children();
foreach(auto obj,t){
qDebug()<<"m_frame children name is "<<obj->objectName();
}
m_rcNormal = rtMainWindow;
this->centralWidget()->raise();
this->centralWidget()->setGeometry(rtCentralGeo);
@ -433,9 +505,7 @@ void QssMainWindow::onMouseMoveEvent( QMouseEvent * ev )
m_frame->show();
this->setGeometry(rtMainWindow);
this->show();
qDebug()<<"parent is "<<this->parent();
qDebug()<<"resize set geometry "<<rc.bottom();
m_rect = rc;
mFrameRect = rc;
m_CentralRect = rtCentralGeo;
m_pos = ev->globalPos();
}
@ -465,11 +535,9 @@ void QssMainWindow::onMouseMoveEvent( QMouseEvent * ev )
void QssMainWindow::onMousePressEvent( QMouseEvent * ev )
{
qDebug("mouse press");
m_pos = ev->globalPos();
this->show();
m_rect = m_frame->geometry();
mFrameRect = m_frame->geometry();
if(nullptr != centralWidget()){
m_CentralRect = centralWidget()->geometry();
}
@ -485,9 +553,9 @@ void QssMainWindow::onMousePressEvent( QMouseEvent * ev )
setCursor(Qt::ArrowCursor);
}
}
m_rtPreGeometry = m_rect;
qDebug("frame geometry is %d %d %d %d",m_rect.x(),m_rect.y(),
m_rect.width(),m_rect.height());
m_rtPreGeometry = mFrameRect;
qDebug("frame geometry is %d %d %d %d",mFrameRect.x(),mFrameRect.y(),
mFrameRect.width(),mFrameRect.height());
}
void QssMainWindow::onMouseReleaseEvent( QMouseEvent * ev )
@ -568,11 +636,11 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev )
m_frame->setCursor(Qt::ArrowCursor);
}
if(QEvent::Resize == ev->type()){
qDebug()<<"resized sended"<<this->objectName();
QRect rtTempGeometry = this->frameGeometry();
qDebug("re geomotry size is %d %d %d %d",rtTempGeometry.x(),
rtTempGeometry.y(),rtTempGeometry.width(),rtTempGeometry.height());
this->setGeometry(rtTempGeometry.x(),rtTempGeometry.y(),rtTempGeometry.width(),rtTempGeometry.height());
//qDebug()<<"resized sended"<<this->objectName();
//QRect rtTempGeometry = this->frameGeometry();
//qDebug("re geomotry size is %d %d %d %d",rtTempGeometry.x(),
// rtTempGeometry.y(),rtTempGeometry.width(),rtTempGeometry.height());
//this->setGeometry(rtTempGeometry.x(),rtTempGeometry.y(),rtTempGeometry.width(),rtTempGeometry.height());
}
else if (ev->type() == QEvent::MouseButtonRelease)
{
@ -706,12 +774,12 @@ void QssDialog::onMouseMoveEvent( QMouseEvent * ev )
return;
if ((m_top || m_bottom) && qAbs(dy) < 5)
return;
if (m_left && dx > 0 && m_rect.width() <= m_frame->minimumWidth())
if (m_left && dx > 0 && mFrameRect.width() <= m_frame->minimumWidth())
return ;
if (m_top && dy > 0 && m_rect.height() <= m_frame->minimumHeight())
if (m_top && dy > 0 && mFrameRect.height() <= m_frame->minimumHeight())
return;
QRect rc = m_rect;
QRect rc = mFrameRect;
if (m_left)
rc.setLeft(rc.left() + dx);
if (m_right)
@ -722,7 +790,7 @@ void QssDialog::onMouseMoveEvent( QMouseEvent * ev )
rc.setBottom(rc.bottom() + dy);
m_frame->setGeometry(rc);
m_rect = rc;
mFrameRect = rc;
m_pos = ev->globalPos();
}
else
@ -763,12 +831,12 @@ void QssDockWidget::onMouseMoveEvent( QMouseEvent * ev )
return;
if ((m_top || m_bottom) && qAbs(dy) < 5)
return;
if (m_left && dx > 0 && m_rect.width() <= m_frame->minimumWidth())
if (m_left && dx > 0 && mFrameRect.width() <= m_frame->minimumWidth())
return ;
if (m_top && dy > 0 && m_rect.height() <= m_frame->minimumHeight())
if (m_top && dy > 0 && mFrameRect.height() <= m_frame->minimumHeight())
return;
QRect rc = m_rect;
QRect rc = mFrameRect;
if (m_left)
rc.setLeft(rc.left() + dx);
if (m_right)
@ -779,7 +847,7 @@ void QssDockWidget::onMouseMoveEvent( QMouseEvent * ev )
rc.setBottom(rc.bottom() + dy);
m_frame->setGeometry(rc);
m_rect = rc;
mFrameRect = rc;
m_pos = ev->globalPos();
}
else
@ -809,7 +877,7 @@ void QssDockWidget::onMouseMoveEvent( QMouseEvent * ev )
void QssDialog::onMousePressEvent( QMouseEvent * ev )
{
m_pos = ev->globalPos();
m_rect = m_frame->geometry();
mFrameRect = m_frame->geometry();
if(m_left || m_right || m_top || m_bottom)
{
m_mousePressedInBorder = ev->button() == Qt::LeftButton;
@ -821,7 +889,7 @@ void QssDialog::onMousePressEvent( QMouseEvent * ev )
void QssDockWidget::onMousePressEvent( QMouseEvent * ev )
{
m_pos = ev->globalPos();
m_rect = m_frame->geometry();
mFrameRect = m_frame->geometry();
if(m_left || m_right || m_top || m_bottom)
{
m_mousePressedInBorder = ev->button() == Qt::LeftButton;

View File

@ -30,6 +30,8 @@ class QssTtitleBar : public QWidget
{
Q_OBJECT
signals:
void OnMaxOrRestore(bool );
public:
enum QTitleBar_Button
{
@ -92,12 +94,11 @@ class QssMainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void OnMaxOrRestore(bool max);
public:
QssMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0);
virtual ~QssMainWindow()
{
m_frame->deleteLater();
}
virtual ~QssMainWindow();
void show();
void showMinimized();
@ -119,17 +120,19 @@ private:
void onMouseMoveEvent(QMouseEvent * ev);
void onMousePressEvent(QMouseEvent * ev);
void onMouseReleaseEvent(QMouseEvent * ev);
protected:
QFrame* m_frame;
QRect mFrameRect;
QRect mRect;
QRect m_CentralRect;
QssTtitleBar* m_titleBar;
private:
QFrame* m_frame;
QssTtitleBar* m_titleBar;
QRect m_rcValid;//桌面最大可用尺寸
QRect m_rcNormal;//还原后窗口尺寸
/** 边框调整大小相关 */
QRect m_rect;
QRect m_CentralRect;
QPoint m_pos;
bool m_mousePressedInBoundy;
bool m_left,m_right,m_top,m_bottom;
@ -176,7 +179,7 @@ private:
QWidget* m_parent;
/** 边框调整大小相关 */
QRect m_rect;
QRect mFrameRect;
QPoint m_pos;
bool m_mousePressedInBorder;
bool m_left,m_right,m_top,m_bottom;
@ -220,7 +223,7 @@ private:
QWidget* m_parent;
/** 边框调整大小相关 */
QRect m_rect;
QRect mFrameRect;
QPoint m_pos;
bool m_mousePressedInBorder;
bool m_left,m_right,m_top,m_bottom;

View File

@ -45,6 +45,7 @@
<file>icon/int.svg</file>
<file>icon/objects.svg</file>
<file>icon/string.svg</file>
<file>icon/最大化.svg</file>
<file>icon/missing.svg</file>
</qresource>
</RCC>

View File

@ -1,5 +1,6 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
MainWindow::MainWindow(QWidget *parent) :
QssMainWindow(parent),
@ -13,6 +14,15 @@ MainWindow::MainWindow(QWidget *parent) :
mAudioCapture(nullptr)
{
ui->setupUi(this);
this->move(50,50);
int i(0);
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect clientRect = desktopWidget->availableGeometry();
QRect applicationRect = desktopWidget->screenGeometry();
qDebug()<<this->pos()<<clientRect<<applicationRect<<this->m_frame->geometry()<<this->centralWidget()->geometry();
this->centralWidget()->setStyleSheet("background:green;");
std::vector<std::wstring> cameras = Camera::EnumAllCamera();
for(std::wstring x : cameras){
ui->comboBox->addItem(QString::fromWCharArray(x.c_str(),x.size()),
@ -22,8 +32,8 @@ MainWindow::MainWindow(QWidget *parent) :
mMic = mAudioCapture->EnumSpeakers();
qDebug()<<"capture "<<mMic.size()<<"mic";
for(vector<CaptureAudioFfmpeg::MICInfo>::iterator itr = mMic.begin();itr != mMic.end();itr++){
ui->comboBox_2->addItem(QString::fromLocal8Bit(itr->name.c_str(),itr->name.size()),
QString::fromLocal8Bit(itr->name.c_str(),itr->name.size()));
}
//mAudioCapture->SetObserver(mAudioCoder);
//mAudioCapture->InitCapture(0, 44100, 2);
@ -56,6 +66,7 @@ void MainWindow::on_pushButton_clicked(){
m_bCameraOpen = true;
mPlayerWidget->show();
ui->verticalLayout->addWidget(mPlayerWidget);
qDebug()<<ui->verticalLayout->layout();
ui->verticalLayout->setStretch(0,1);
ui->verticalLayout->setStretch(1,0);
ui->verticalLayout->setStretch(2,9);

View File

@ -172,24 +172,6 @@
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1383</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>