fix windows multi monitor full screen

This commit is contained in:
shun-iwasawa 2021-03-04 12:56:10 +09:00 committed by manongjohn
parent 6a03522d1d
commit dbd5cf1001

View file

@ -904,28 +904,24 @@ void FullScreenWidget::setWidget(QWidget *widget) {
if ((m_widget = widget)) layout->addWidget(m_widget); if ((m_widget = widget)) layout->addWidget(m_widget);
} }
//============================================================= //=============================================================
bool FullScreenWidget::toggleFullScreen( bool FullScreenWidget::toggleFullScreen(
//============================================================= //=============================================================
const bool kfApplicationQuitInProgress ) // Indicates whether the application is quiting. const bool kfApplicationQuitInProgress) // Indicates whether the
// application is quiting.
/* /*
* DESCRIPTION: * DESCRIPTION:
* *
* Sets the size and location of the widget to either full * Entering full screen has to be done manually in order to avoid having the
* screen or normal. * window placed on the wrong monitor on systems running X11 with multiple
* * monitors.
* Entering full screen has to be done manually in order to
* avoid having the window placed on the wrong monitor on
* systems running X11 with multiple monitors.
*/ */
{ {
// Initialize the return value. // Initialize the return value.
bool fFullScreenStateToggled = false; bool fFullScreenStateToggled = false;
// Define some constants for setting and clearing window flags. // Define some constants for setting and clearing window flags.
const Qt::WindowFlags kwfFullScreenWidgetFlags = const Qt::WindowFlags kwfFullScreenWidgetFlags =
Qt::Window | // <-- Make the widget become a window. Qt::Window | // <-- Make the widget become a window.
@ -933,12 +929,11 @@ void FullScreenWidget::setWidget(QWidget *widget) {
Qt::FramelessWindowHint; // <-- Full screen windows have no border. Qt::FramelessWindowHint; // <-- Full screen windows have no border.
const Qt::WindowFlags kwfFullScreenWidgetExcludedFlagsMask = const Qt::WindowFlags kwfFullScreenWidgetExcludedFlagsMask =
(Qt::WindowFlags)~Qt::WindowTitleHint; // <-- Full screen windows have no titlebar. (Qt::WindowFlags)~Qt::WindowTitleHint; // <-- Full screen windows have no
// titlebar.
// Determine whether to enter or leave full screen mode // Determine whether to enter or leave full screen mode
if (this->windowState() & Qt::WindowFullScreen) if (this->windowState() & Qt::WindowFullScreen) {
{
this->hide(); this->hide();
this->setWindowFlags(this->windowFlags() & ~kwfFullScreenWidgetFlags); this->setWindowFlags(this->windowFlags() & ~kwfFullScreenWidgetFlags);
@ -947,67 +942,59 @@ void FullScreenWidget::setWidget(QWidget *widget) {
this->m_widget->setFocus(); this->m_widget->setFocus();
// Set the return value to indicate that the full screen mode has been
// Set the return value to indicate that the full screen mode has been changed. // changed.
fFullScreenStateToggled = true; fFullScreenStateToggled = true;
} } else {
else
{
// There's no point to switching into full screen if the // There's no point to switching into full screen if the
// application is in the process of quiting. // application is in the process of quiting.
if (!kfApplicationQuitInProgress) if (!kfApplicationQuitInProgress) {
{
#if !defined( _WIN32 )
//============================================================== //==============================================================
// //
// NOTE: // NOTE:
// //
// This new way of going into full screen mode does // This new way of going into full screen mode does things manually in
// things manually in order to bypass Qt's incorrect // order to bypass Qt's incorrect policy of always relocating a widget
// policy of always relocating a widget to desktop // to desktop coordinates (0,0) when the widget becomes a window via
// coordinates (0,0) when the widget becomes a window // setting the Qt::Window flag.
// via setting the Qt::Window flag.
// //
// This makes full screen mode work MUCH better on X11 // This makes full screen mode work MUCH better on X11 systems with
// systems with multiple displays. // multiple displays.
// //
//============================================================== //==============================================================
// //
// STRATEGY: // STRATEGY:
// //
// 1. Obtain the rectangle of the screen that the widgets host window is on. // 1. Obtain the rectangle of the screen that the widgets host
// This has to be done first, otherwise the CORRECT screen info can // window is on. This has to be done first, otherwise the CORRECT
// potentially become unavailable in the following steps. // screen info can potentially become unavailable in the following
// steps.
// //
// 2. Manually set all the necessary flags for the full screen window // 2. Manually set all the necessary flags for the full screen
// attributes and state. Qt WILL hide the widget/window when this is done. // window attributes and state. Qt WILL hide the widget/window when
// this is done.
// //
// 3. Set the window geometry/rect to be the same as the screen from Step 1. // 3. Set the window geometry/rect to be the same as the screen from
// Step 1.
// //
// 4. Make the window visible again. // 4. Make the window visible again.
// //
//
//--------------------------------------------------- //---------------------------------------------------
// STEP 1: // STEP 1:
// Get the window widget that contains this widget. // Get the window widget that contains this widget.
QWidget *ptrWindowWidget = this->window(); QWidget *ptrWindowWidget = this->window();
if (ptrWindowWidget) if (ptrWindowWidget) {
{
// Get the access to the QWindow object of the containing window. // Get the access to the QWindow object of the containing window.
QWindow *ptrContainingQWindow = ptrWindowWidget->windowHandle(); QWindow *ptrContainingQWindow = ptrWindowWidget->windowHandle();
if (ptrContainingQWindow) if (ptrContainingQWindow) {
{
// Get access to the screen the window is on. // Get access to the screen the window is on.
QScreen *ptrScreenThisWindowIsOn = ptrContainingQWindow->screen(); QScreen *ptrScreenThisWindowIsOn = ptrContainingQWindow->screen();
if (ptrScreenThisWindowIsOn) if (ptrScreenThisWindowIsOn) {
{ #if !defined(_WIN32)
// Get the geometry rect for the correct screen. // Get the geometry rect for the correct screen.
QRect qrcScreen = ptrScreenThisWindowIsOn->geometry(); QRect qrcScreen = ptrScreenThisWindowIsOn->geometry();
//--------------------------------------------------- //---------------------------------------------------
// STEP 2: // STEP 2:
@ -1019,50 +1006,50 @@ void FullScreenWidget::setWidget(QWidget *widget) {
// //
this->setWindowFlags( this->setWindowFlags(
(this->windowFlags() & kwfFullScreenWidgetExcludedFlagsMask) | (this->windowFlags() & kwfFullScreenWidgetExcludedFlagsMask) |
kwfFullScreenWidgetFlags kwfFullScreenWidgetFlags);
);
// Set the window state flag to indicate that it's now in fullscreen mode. // Set the window state flag to indicate that it's now in fullscreen
// // mode.
// If this state flag isn't set, the test for whether to enter or leave // If this state flag isn't set, the test for whether to enter or
// full screen mode won't work correctly. // leave full screen mode won't work correctly.
this->setWindowState(Qt::WindowFullScreen); this->setWindowState(Qt::WindowFullScreen);
//--------------------------------------------------- //---------------------------------------------------
// STEP 3: // STEP 3:
// Set the window to the geometry rect of the correct screen. // Set the window to the geometry rect of the correct screen.
this->setGeometry(qrcScreen); this->setGeometry(qrcScreen);
//--------------------------------------------------- //---------------------------------------------------
// STEP 4: // STEP 4:
// Make the window visible. This also causes all the changes to the widget's // Make the window visible. This also causes all the changes to the
// flags, state and geometry that was just set to take effect. // widget's flags, state and geometry that was just set to take
// effect.
this->show(); this->show();
}
}
}
#else #else
this->setWindowFlags( this->windowFlags() | Qt::Window | Qt::WindowStaysOnTopHint ); this->setWindowFlags(this->windowFlags() | Qt::Window |
Qt::WindowStaysOnTopHint);
this->window()->windowHandle()->setScreen(ptrScreenThisWindowIsOn);
// http://doc.qt.io/qt-5/windows-issues.html#fullscreen-opengl-based-windows // http://doc.qt.io/qt-5/windows-issues.html#fullscreen-opengl-based-windows
this->winId(); this->winId();
QWindowsWindowFunctions::setHasBorderInFullScreen( this->windowHandle(), true ); QWindowsWindowFunctions::setHasBorderInFullScreen(
this->windowHandle(), true);
this->showFullScreen(); this->showFullScreen();
#endif #endif
}
}
}
// Set the return value to indicate that the full screen mode has been
// Set the return value to indicate that the full screen mode has been changed. // changed.
fFullScreenStateToggled = true; fFullScreenStateToggled = true;
} }
} }
return (fFullScreenStateToggled); return (fFullScreenStateToggled);
} }
} // imageutils } // namespace ImageUtils