Owen
Owen

Reputation: 4173

Make a Qt window autofit to the screen's size

I have a Qt application which needs to be loaded on mobile devices of different screen sizes. How do I make it autofit to the mobile device's screen size?

Upvotes: 15

Views: 31279

Answers (3)

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14941

If you really want the geometry, you could use QDesktopWidget to get information about the display, including the geometry of it.

If you just want the window to be sized properly, however, you should use QWidget::setWindowState, as Andrew suggested.

Upvotes: 6

Gareth Stockwell
Gareth Stockwell

Reputation: 3122

If you want your application's main window to occupy the whole screen as soon as it starts, use QWidget::showMaximized, e.g.

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    MyMainWidget widget;
    widget.showMaximized();
    return app.exec();
}

Note that showMaximized is a convenience function which internally calls the QWidget::setWindowState mentioned by Andrew:

void QWidget::showMaximized()
{
    // ...
    setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
                   | Qt::WindowMaximized);
    show();
}

Upvotes: 17

Andrew
Andrew

Reputation: 24846

void QWidget::setWindowState ( Qt::WindowStates windowState )

Sets the window state to windowState. The window state is a OR'ed combination of Qt::WindowState: Qt::WindowMinimized, Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.

From documentation of QWidget. Hope it will help

Upvotes: 3

Related Questions