'Qt FullScreen on Startup
I want to start an application on fullscreen (MacOS 10.8.x, Qt 5.1.1, C++) depending on the settings:
main.cpp
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showFullScreen();
return a.exec();
}
MainWindow.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
The settings comportment is perfect, works like a charm. But this->showFullScreen() does something very ugly :
- Display the window in fullscreen
- Display the window in normal size in the center
- Scale the window to fullscreen
How to avoid this?
Solution 1:[1]
use the following if you want to have the app open as maximized window:
Mainwindow w;
w.setWindowState(Qt::WindowMaximized);
w.show();
use the following if you want to have the app open as fullscreen window:
Mainwindow w;
w.setWindowState(Qt::WindowFullScreen);
w.show();
Solution 2:[2]
You can try QMainWindow::showFullScreen() in the constructor:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMainWindow::showFullScreen();
}
Solution 3:[3]
QWidget states should help you. Follow this Qt-documentation: QWidget::setWindowState.
Way to put app into fullscreen:
MainWindow w;
w.setWindowState(w.windowState() ^ Qt::WindowFullScreen);
Thanks
Solution 4:[4]
Try to call showFullScreen() in showEvent(). I can't check it with Qt 5.1.1 currently but it worked quite well with 4.8.x
Solution 5:[5]
I'm using a QWidget as my main window, but I got it to work perfectly with this simple line:
this->showMaximized();
Upon running, it appeared filling the entire screen immediately and smoothly.
Solution 6:[6]
What I did was just delay the Fullscreen switch by 1 second with a Qtimer. It's not the best solution, but it does fix the problem. Good luck!
Solution 7:[7]
I found a way today, it seems best for me until now.. I tried many other solutions nothing worked.
What I do is;
getting available screen resolution clearly.
resizing the window simply before showinf window
showing the window normally
Keep in mind, if you do showWindow() before setting the window resize, and if you have a scene in main program then the scroll will not be on center because of resize. Also the window may not be positioned correctly. So firstly setFixedSize() and ShowNormal() as below..
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
WhateverMainWindowClass *w = new WhateverMainWindowClass();
/////////////////////////
QRect screenGeometry = QApplication::desktop()->availableGeometry();
w->resize(screenGeometry.width(), screenGeometry.height());
w->showNormal();
/////////////////////////
return app.exec();
}
in addition you can make setFixedSize() instead of resize(), just with exactly same way, so then the size will be static, no one can change it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zhang Tianbao |
| Solution 2 | Alexis Wilke |
| Solution 3 | Alexander Borodulya |
| Solution 4 | ixSci |
| Solution 5 | Zac |
| Solution 6 | ultimatetechie |
| Solution 7 | Alexis Wilke |
