- Switch to using QGuiApplication::primaryScreen for screen size (desktopwidget is deprecated)

- connect() to the geometryChanged and orientationChanged screen calls to detect changes
This commit is contained in:
Alfred Reynolds
2021-08-06 16:50:54 +12:00
parent eac73d618b
commit a2c452fdcd

View File

@@ -15,7 +15,8 @@
#include <QRect> #include <QRect>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsPixmapItem> #include <QGraphicsPixmapItem>
#include <QDesktopWidget> #include <QApplication>
#include <QScreen>
#include <sstream> #include <sstream>
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
@@ -34,6 +35,16 @@ MainWindow::MainWindow(QWidget *parent) :
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
update(); update();
QScreen* screen = QGuiApplication::primaryScreen();
if (screen)
{
connect(screen, SIGNAL(geometryChanged(QRect)), this, SLOT(checkWindowSize()));
connect(screen, SIGNAL(orientationChanged(Qt::ScreenOrientation)), this, SLOT(checkWindowSize()));
screen->setOrientationUpdateMask(Qt::LandscapeOrientation |
Qt::PortraitOrientation |
Qt::InvertedLandscapeOrientation |
Qt::InvertedPortraitOrientation);
}
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@@ -107,9 +118,11 @@ bool MainWindow::event(QEvent* event)
void MainWindow::resizeEvent(QResizeEvent* event) void MainWindow::resizeEvent(QResizeEvent* event)
{ {
QMainWindow::resizeEvent(event); QMainWindow::resizeEvent(event);
// the window size in this event may not match the monitor size, so use QDesktopWidget to find the true monitor size // the window size in this event may not match the monitor size, so use QGuiApplication to find the true monitor size
QDesktopWidget desktop; QScreen *screen = QGuiApplication::primaryScreen();
QSize screenSize = desktop.screenGeometry(this).size(); if (screen != nullptr)
{
QSize screenSize = screen->geometry().size();
bool isLandscape = screenSize.width() > screenSize.height(); bool isLandscape = screenSize.width() > screenSize.height();
if (imageAspectMatchesMonitor) if (imageAspectMatchesMonitor)
{ {
@@ -120,25 +133,32 @@ void MainWindow::resizeEvent(QResizeEvent* event)
std::cout << "Got resize:" << this << " " << screenSize.width() << "," << screenSize.height() << " , is landscape? " << (isLandscape ? "y" : "n") << std::endl; std::cout << "Got resize:" << this << " " << screenSize.width() << "," << screenSize.height() << " , is landscape? " << (isLandscape ? "y" : "n") << std::endl;
} }
updateImage(true); updateImage(true);
QTimer::singleShot(5, this, SLOT(checkWindowSize()));
if(lastScreenSize != screenSize && switcher != nullptr) if(lastScreenSize != screenSize && switcher != nullptr)
{ {
lastScreenSize = screenSize; lastScreenSize = screenSize;
if(debugMode)
{
std::cout << "Updating image due to resize" << std::endl;
}
switcher->updateImage(); switcher->updateImage();
} }
}
} }
void MainWindow::checkWindowSize() void MainWindow::checkWindowSize()
{ {
QDesktopWidget desktop; QScreen *screen = QGuiApplication::primaryScreen();
QRect screenSize = desktop.screenGeometry(this); if (screen != nullptr)
if(size() != screenSize.size()) {
QSize screenSize = screen->geometry().size();
if(size() != screenSize)
{ {
if(debugMode) if(debugMode)
{ {
std::cout << "Resizing Window" << screenSize.width() << "," << screenSize.height() << std::endl; std::cout << "Resizing Window" << screenSize.width() << "," << screenSize.height() << std::endl;
} }
setFixedSize(screenSize.size()); setFixedSize(screenSize);
}
} }
} }