Fix bug155583 bug154391 QMessagebox未居中问题处理 QDialog在设置parent的情况

This commit is contained in:
谭静 2023-01-11 16:13:50 +08:00
parent adaeb641d5
commit 02f4d8e361
1 changed files with 65 additions and 16 deletions

View File

@ -32,8 +32,6 @@
#include <QStyleOption> #include <QStyleOption>
#include <qpa/qplatformdialoghelper.h> #include <qpa/qplatformdialoghelper.h>
#include <QtWidgets/qdialogbuttonbox.h> #include <QtWidgets/qdialogbuttonbox.h>
#include "ukuistylehelper/ukuistylehelper.h"
#include "windowmanager/windowmanager.h"
#include "private/qlabel_p.h" #include "private/qlabel_p.h"
#include "private/qdialog_p.h" #include "private/qdialog_p.h"
@ -493,11 +491,11 @@ void MessageBox::setWindowModality(Qt::WindowModality windowModality)
{ {
QDialog::setWindowModality(windowModality); QDialog::setWindowModality(windowModality);
if (parentWidget() && windowModality == Qt::WindowModal) { // if (parentWidget() && windowModality == Qt::WindowModal) {
setParent(parentWidget(), Qt::Sheet); // setParent(parentWidget(), Qt::Sheet);
} else { // } else {
setParent(parentWidget(), Qt::Dialog); // setParent(parentWidget(), Qt::Dialog);
} // }
setDefaultButton(d_func()->mDefaultButton); setDefaultButton(d_func()->mDefaultButton);
} }
@ -709,6 +707,7 @@ MessageBoxPrivate::MessageBoxPrivate() : mCheckbox(nullptr), mEscapeButton(nullp
MessageBoxPrivate::~MessageBoxPrivate() MessageBoxPrivate::~MessageBoxPrivate()
{ {
/*
if (nullptr != mLabel) { if (nullptr != mLabel) {
delete mLabel; delete mLabel;
} }
@ -728,6 +727,7 @@ MessageBoxPrivate::~MessageBoxPrivate()
if (nullptr != mDetailButton) { if (nullptr != mDetailButton) {
delete mDetailButton; delete mDetailButton;
} }
*/
} }
void MessageBoxPrivate::init(const QString &title, const QString &text) void MessageBoxPrivate::init(const QString &title, const QString &text)
@ -1407,15 +1407,68 @@ bool MessageBoxHelper::show(Qt::WindowFlags windowFlags, Qt::WindowModality wind
} }
} }
} }
}
}
for (QMessageBox *mb : p->findChildren<QMessageBox *>()) {
if (mb->icon() == options()->icon() && mb->windowTitle() == options()->windowTitle() && mb->text() == options()->text()
&& mb->informativeText() == options()->informativeText() && mb->detailedText() == options()->detailedText()) {
if (mb->parentWidget() != nullptr && windowModality == Qt::WindowModal) {
///QDialog center in parent
mMessageBox->setParent(p, Qt::Sheet);
}
else{
///QDialog center in parent
mMessageBox->setParent(p, Qt::Dialog);
}
}
}
}
}
else{
if(windowModality == Qt::WindowModal)
mMessageBox->setWindowFlag(Qt::Sheet);
else
mMessageBox->setWindowFlag(Qt::Dialog);
}
mMessageBox->setuplayout(); mMessageBox->setuplayout();
if (parent && !mMessageBox->isVisible()) { /*
mMessageBox->move(QPoint((parent->width() - mMessageBox->width()) / 2, (parent->height() - mMessageBox->height()) / 2) if (!mMessageBox->isVisible()) {
+ QPoint(parent->x(), parent->y())); if(parent){
QWidget *p = mMessageBox->find(parent->winId());
int x = (p->width() - mMessageBox->width()) > 0 ? (p->width() - mMessageBox->width()) / 2 : 0;
int y = (p->height() - mMessageBox->height()) > 0 ? (p->height() - mMessageBox->height()) / 2 : 0;
QPoint gloabP = QPoint(x, y) + p->mapToGlobal(p->pos());
qDebug() << "gloabP...." << gloabP;
QPoint point = p->mapFromGlobal(gloabP);
qDebug() << "point....." << point;
if (windowModality == Qt::WindowModal) {
qDebug() << "WindowModal............";
mMessageBox->setParent(p, Qt::Sheet);
}
else{
qDebug() << "Dialog............";
mMessageBox->setParent(p, Qt::Dialog);
}
// mMessageBox->move(point);
qDebug() << "mMessageBox parent......" << p << p->geometry() << p->mapToGlobal(p->pos());
qDebug() << "mMessageBox ....." << mMessageBox << mMessageBox->geometry();
}
else{
int number = QApplication::desktop()->screenNumber(QCursor::pos());
if(number<0){
number=0;
}
QSize size = QApplication::screens().at(number)->availableGeometry().size();
// qDebug() << "availableGeometry......" << size << QApplication::screens().at(number)->availableSize();
mMessageBox->move(QPoint((size.width() - mMessageBox->width()) / 2, (size.height() - mMessageBox->height()) / 2));
}
// if (QWidget *p = mMessageBox->find(parent->winId())) { // if (QWidget *p = mMessageBox->find(parent->winId())) {
// qDebug() << "isvisible....." << mMessageBox->isVisible(); // qDebug() << "isvisible....." << mMessageBox->isVisible();
@ -1430,6 +1483,7 @@ bool MessageBoxHelper::show(Qt::WindowFlags windowFlags, Qt::WindowModality wind
// qDebug() << "parent11111111............" << mMessageBox->geometry() << parent->geometry() << p->geometry(); // qDebug() << "parent11111111............" << mMessageBox->geometry() << parent->geometry() << p->geometry();
// } // }
} }
*/
QString platform = QGuiApplication::platformName(); QString platform = QGuiApplication::platformName();
if(platform.startsWith(QLatin1String("wayland"),Qt::CaseInsensitive)) if(platform.startsWith(QLatin1String("wayland"),Qt::CaseInsensitive))
@ -1452,11 +1506,6 @@ bool MessageBoxHelper::show(Qt::WindowFlags windowFlags, Qt::WindowModality wind
} }
} }
mMessageBox->show(); mMessageBox->show();
Q_UNUSED(parent);
Q_UNUSED(windowFlags);
Q_UNUSED(windowModality);
return true; return true;
} }