How to fix inconsistent window placement?
-
Hello everybody!
I am using Qt 6.8b2 (same happens with Qt 6.7.2 as well) both on Windows 11 and macOS Sonoma 14.5 and I am currently unable to fix a - for me - annoying inconsistency when trying to place a
QTextEdit()
in the lower left corner of a single screen:m_logWindow = new QTextEdit(); m_logWindow->setWindowTitle("Log Output"); m_logWindow->setReadOnly(true); m_logWindow->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); m_logWindow->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint ); const auto size = m_logWindow->screen()->availableSize(); m_logWindow->resize(static_cast<int>(std::round(size.width() * 0.75)), 240); m_logWindow->show(); m_logWindow->move(0, size.height() - m_logWindow->frameGeometry().height());
On Windows 11 the result is as expected:
On macOS, the same code however produces a "large" gap between the window and the macOS dock (red rectangle):
How could I achieve consistent window placement?
-
@mpergand using
size()
would discard the height of the bottom dock and place the window behind it.
However I found the solution to correctly calculate the position by usingavailableVirtualGeometry()
. The returnedQRect
does take into account the top menu bar height and the calculation basically is now as follows:const auto geom = logWindow->screen()->availableVirtualGeometry(); //qDebug() << "avail virtual geo: " << logWindow->screen()->availableVirtualGeometry(); logWindow->resize(static_cast<int>(std::round(geom.width() * 0.75)), 240); logWindow->show(); logWindow->move(0, geom.height() + geom.y() - logWindow->frameGeometry().height());
This does work on both platforms.
-
Hello everybody!
I am using Qt 6.8b2 (same happens with Qt 6.7.2 as well) both on Windows 11 and macOS Sonoma 14.5 and I am currently unable to fix a - for me - annoying inconsistency when trying to place a
QTextEdit()
in the lower left corner of a single screen:m_logWindow = new QTextEdit(); m_logWindow->setWindowTitle("Log Output"); m_logWindow->setReadOnly(true); m_logWindow->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); m_logWindow->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint ); const auto size = m_logWindow->screen()->availableSize(); m_logWindow->resize(static_cast<int>(std::round(size.width() * 0.75)), 240); m_logWindow->show(); m_logWindow->move(0, size.height() - m_logWindow->frameGeometry().height());
On Windows 11 the result is as expected:
On macOS, the same code however produces a "large" gap between the window and the macOS dock (red rectangle):
How could I achieve consistent window placement?
Hi @DerReisende
const auto size = m_logWindow->screen()->availableSize();
I think with availableSize you don't take the top menu in account.
Use instead:m_logWindow->screen()->size();
-
@mpergand using
size()
would discard the height of the bottom dock and place the window behind it.
However I found the solution to correctly calculate the position by usingavailableVirtualGeometry()
. The returnedQRect
does take into account the top menu bar height and the calculation basically is now as follows:const auto geom = logWindow->screen()->availableVirtualGeometry(); //qDebug() << "avail virtual geo: " << logWindow->screen()->availableVirtualGeometry(); logWindow->resize(static_cast<int>(std::round(geom.width() * 0.75)), 240); logWindow->show(); logWindow->move(0, geom.height() + geom.y() - logWindow->frameGeometry().height());
This does work on both platforms.
-