C++ 启动时 Qt 全屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19817881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt FullScreen on Startup
提问by Thomas Ayoub
I want to start an application on fullscreen (MacOS 10.8.x, Qt 5.1.1, C++) depending on the settings:
我想根据设置在全屏(MacOS 10.8.x、Qt 5.1.1、C++)上启动应用程序:
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
主窗口.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 :
设置规范是完美的,就像一个魅力。但是this->showFullScreen()
做了一些非常难看的事情:
- Display the window in fullscreen
- Display the window in normal size in the center
- Scale the window to fullscreen
- 全屏显示窗口
- 在中心以正常大小显示窗口
- 将窗口缩放到全屏
I can provide a video if needed. How to avoid this?
如果需要,我可以提供视频。如何避免这种情况?
edit :
编辑 :
添加视频 here在这里,在没有设置的情况下制作了一个更好的片段采纳答案by Martin Delille
I already faced this problem and a very nice solution was to delay the fullscreen switch by one second (using a QTimer):
我已经遇到了这个问题,一个非常好的解决方案是将全屏切换延迟一秒(使用QTimer):
QTimer::singleShot(0, this, SLOT(showFullScreen()));
回答by Ervis Tusha
you can try QMainWindow::showFullScreen()
;
你可以试试QMainWindow::showFullScreen()
;
{
ui->setupUi(this);
QMainWindow::showFullScreen();
}
回答by Zhang Tianbao
use the following if you want to have the app open as maximizedwindow:
如果您想让应用程序以最大化窗口打开,请使用以下命令:
Mainwindow w;
w.setWindowState(Qt::WindowMaximized);
w.show();
use the following if you want to have the app open as fullscreenwindow:
如果您想让应用程序以全屏窗口打开,请使用以下命令:
Mainwindow w;
w.setWindowState(Qt::WindowFullScreen);
w.show();
回答by Alexander Borodulya
QWidget states should help you. Follow this Qt-documentation: QWidget::setWindowState.
QWidget 状态应该可以帮助您。遵循这个 Qt 文档:QWidget::setWindowState。
Way to put app into fullscreen:
将应用程序置于全屏模式的方法:
MainWindow w;
w.setWindowState(w.windowState() ^ Qt::WindowFullScreen);
Thanks
谢谢
回答by ixSci
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
试图调用showFullScreen()
在showEvent()
。我目前无法使用 Qt 5.1.1 检查它,但它在 4.8.x 上运行良好
回答by Zac
I'm using a QWidget as my main window, but I got it to work perfectly with this simple line:
我使用 QWidget 作为我的主窗口,但我让它与这个简单的行完美地工作:
this->showMaximized();
Upon running, it appeared filling the entire screen immediately and smoothly.
运行后,它立即顺利地充满了整个屏幕。
回答by ultimatetechie
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!
我所做的只是使用 Qtimer 将全屏切换延迟 1 秒。这不是最好的解决方案,但确实解决了问题。祝你好运!
回答by questioner
I made 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
清楚地获得可用的屏幕分辨率。
在 showinf 窗口之前调整窗口大小
正常显示窗口
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 setFixedSizeand ShowNormalas below..
请记住,如果您在设置窗口调整大小之前执行 showWindow,并且如果您在主程序中有一个场景,那么由于调整大小,滚动将不会位于中心。此外,窗口可能未正确定位。所以首先设置FixedSize和ShowNormal如下..
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 setFixedSizeinstead of resize, just with exactly same way, so then the size will be static, no one can change it..
此外,您可以使用setFixedSize而不是resize,只是使用完全相同的方式,因此大小将是静态的,没有人可以更改它..