Search code examples
qtwindowqwidget

Child widget will be missing when QMainWindow is Non-Maximized


ui->setupUi(this);
setWindowState(Qt::WindowMaximized);//Qt::WindowFullScreen;

QWidget *pStateInfosBar = new StateInfosBar(this);
//pStateInfosBar->show();
QRect rcSibWannaGeometry(QApplication::desktop()->width() - 250,
                         style()->PixelMetric::PM_TitleBarHeight, 250,
                         height());
pStateInfosBar->setGeometry(rcSibWannaGeometry);

My codes as shown above.But the pStateInfosBar will be missing when his parent window non-maximized, and it will reappear when parent is maximized.Can anybody tell me why?


Solution

  • In your initialization of the QRect, you're setting the x coordinate of pStateInfosBar to your screen's width - 250. As a result, if your window's width is less than your screen's width - 250, the widget will be outside the window.

    Maybe you were looking for this->width() - 250 instead of QApplication::desktop()->width() - 250