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,165 +904,152 @@ 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. Qt::WindowStaysOnTopHint | // <-- Ensure the window stays on top.
Qt::WindowStaysOnTopHint | // <-- Ensure the window stays on top. 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
(Qt::WindowFlags)~Qt::WindowTitleHint; // <-- Full screen windows have no titlebar. // 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 ); this->showNormal();
this->showNormal(); this->m_widget->setFocus();
this->m_widget->setFocus(); // Set the return value to indicate that the full screen mode has been
// changed.
fFullScreenStateToggled = true;
// Set the return value to indicate that the full screen mode has been changed. } else {
fFullScreenStateToggled = true; // There's no point to switching into full screen if the
} // application is in the process of quiting.
else if (!kfApplicationQuitInProgress) {
{ //==============================================================
// There's no point to switching into full screen if the //
// application is in the process of quiting. // NOTE:
if (!kfApplicationQuitInProgress) //
{ // This new way of going into full screen mode does things manually in
#if !defined( _WIN32 ) // order to bypass Qt's incorrect policy of always relocating a widget
//============================================================== // to desktop coordinates (0,0) when the widget becomes a window via
// // setting the Qt::Window flag.
// NOTE: //
// // This makes full screen mode work MUCH better on X11 systems with
// This new way of going into full screen mode does // multiple displays.
// things manually in order to bypass Qt's incorrect //
// policy of always relocating a widget to desktop //==============================================================
// coordinates (0,0) when the widget becomes a window //
// via setting the Qt::Window flag. // STRATEGY:
// //
// This makes full screen mode work MUCH better on X11 // 1. Obtain the rectangle of the screen that the widgets host
// systems with multiple displays. // window is on. This has to be done first, otherwise the CORRECT
// // screen info can potentially become unavailable in the following
//============================================================== // steps.
// //
// STRATEGY: // 2. Manually set all the necessary flags for the full screen
// // window attributes and state. Qt WILL hide the widget/window when
// 1. Obtain the rectangle of the screen that the widgets host window is on. // this is done.
// This has to be done first, otherwise the CORRECT screen info can //
// potentially become unavailable in the following steps. // 3. Set the window geometry/rect to be the same as the screen from
// // Step 1.
// 2. Manually set all the necessary flags for the full screen window //
// attributes and state. Qt WILL hide the widget/window when this is done. // 4. Make the window visible again.
// //
// 3. Set the window geometry/rect to be the same as the screen from Step 1. //---------------------------------------------------
// // STEP 1:
// 4. Make the window visible again.
// // Get the window widget that contains this widget.
// QWidget *ptrWindowWidget = this->window();
if (ptrWindowWidget) {
// Get the access to the QWindow object of the containing window.
//--------------------------------------------------- QWindow *ptrContainingQWindow = ptrWindowWidget->windowHandle();
// STEP 1: if (ptrContainingQWindow) {
// Get access to the screen the window is on.
// Get the window widget that contains this widget. QScreen *ptrScreenThisWindowIsOn = ptrContainingQWindow->screen();
QWidget * ptrWindowWidget = this->window(); if (ptrScreenThisWindowIsOn) {
if (ptrWindowWidget) #if !defined(_WIN32)
{ // Get the geometry rect for the correct screen.
// Get the access to the QWindow object of the containing window. QRect qrcScreen = ptrScreenThisWindowIsOn->geometry();
QWindow * ptrContainingQWindow = ptrWindowWidget->windowHandle();
if (ptrContainingQWindow) //---------------------------------------------------
{ // STEP 2:
// Get access to the screen the window is on.
QScreen * ptrScreenThisWindowIsOn = ptrContainingQWindow->screen(); // Set the window flags to be frameless and with no titlebar.
if (ptrScreenThisWindowIsOn) //
{ // This call will turn the widget into a "window", and HIDE it. This
// Get the geometry rect for the correct screen. // is because Qt always hides a widget when it transforms a widget
QRect qrcScreen = ptrScreenThisWindowIsOn->geometry(); // into a window, or turns a window into a widget.
//
this->setWindowFlags(
//--------------------------------------------------- (this->windowFlags() & kwfFullScreenWidgetExcludedFlagsMask) |
// STEP 2: kwfFullScreenWidgetFlags);
// Set the window flags to be frameless and with no titlebar. // Set the window state flag to indicate that it's now in fullscreen
// // mode.
// This call will turn the widget into a "window", and HIDE it. This // If this state flag isn't set, the test for whether to enter or
// is because Qt always hides a widget when it transforms a widget // leave full screen mode won't work correctly.
// into a window, or turns a window into a widget. this->setWindowState(Qt::WindowFullScreen);
//
this->setWindowFlags( //---------------------------------------------------
(this->windowFlags() & kwfFullScreenWidgetExcludedFlagsMask) | // STEP 3:
kwfFullScreenWidgetFlags
); // Set the window to the geometry rect of the correct screen.
this->setGeometry(qrcScreen);
// 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 // STEP 4:
// full screen mode won't work correctly.
this->setWindowState( Qt::WindowFullScreen ); // Make the window visible. This also causes all the changes to the
// widget's flags, state and geometry that was just set to take
// effect.
//--------------------------------------------------- this->show();
// STEP 3:
// Set the window to the geometry rect of the correct screen.
this->setGeometry( qrcScreen );
//---------------------------------------------------
// STEP 4:
// Make the window visible. This also causes all the changes to the widget's
// flags, state and geometry that was just set to take effect.
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 changed. }
fFullScreenStateToggled = true;
} // Set the return value to indicate that the full screen mode has been
} // changed.
fFullScreenStateToggled = true;
}
return( fFullScreenStateToggled ); }
return (fFullScreenStateToggled);
} }
} // imageutils } // namespace ImageUtils