‘QDesktopWidget::availableGeometry‘: Use QGuiApplication::screens()
在较新的 Qt 版本中,QDesktopWidget
类已被标记为过时,推荐使用 QGuiApplication::screens()
来替代获取桌面相关信息。我的原代码中使用了 QApplication::desktop()->availableGeometry()
,这是旧的获取桌面可用区域的方式,因此编译器会给出相应的警告或错误提示。
原本获取桌面相关信息的代码
void BasicWindow::onButtonMaxClicked()
{
_titleBar->saveRestoreInfo(pos(), QSize(width(), height()));
QRect desktopRect = QApplication::desktop()->availableGeometry();
QRect factRect = QRect(desktopRect.x() - 3, desktopRect.y() - 3,
desktopRect.width() + 6, desktopRect.height() + 6);
setGeometry(factRect);
}
修改后的代码:
void BasicWindow::onButtonMaxClicked()
{
_titleBar->saveRestoreInfo(pos(), QSize(width(), height()));
// 获取主屏幕
QScreen *primaryScreen = QGuiApplication::primaryScreen();
if (!primaryScreen) {
return;
}
// 获取主屏幕的可用几何区域
QRect desktopRect = primaryScreen->availableGeometry();
// 扩展可用区域
QRect factRect = QRect(desktopRect.x() - 3, desktopRect.y() - 3,
desktopRect.width() + 6, desktopRect.height() + 6);
setGeometry(factRect);
}