C++ (Qt)void MainWindow::resizeToContents(){ if (isMaximized()) return; auto nonogramSize = ui->gridWidget->nonogramSize(); int addWidth = width() - ui->scrollArea->width(), addHeight = height() - ui->scrollArea->height(); auto desktopRect = qApp->desktop()->availableGeometry(); resize(desktopRect.size().boundedTo(QSize(nonogramSize.width() + addWidth, nonogramSize.height() + addHeight))); auto frame = frameGeometry(); int maxX = frame.x() + frame.width(), desktopMaxX = desktopRect.x() + desktopRect.width(); int maxY = frame.y() + frame.height(), desktopMaxY = desktopRect.y() + desktopRect.height(); auto p = pos(); if (maxX > desktopMaxX) p.rx() -= maxX - desktopMaxX; if (maxY > desktopMaxY) p.ry() -= maxY - desktopMaxY; move(p);}
C++ (Qt)QRect R = qApp->desktop()->availableGeometry(); QWidget * doomed = new QWidget;doomed->setGeometry(R);doomed->winId(); // create system windowQMargins mg = doomed->windowHandle()->frameMargins();delete doomed; R = R.marginsRemoved(mg);this->setGeometry(R);