c++ - Qt - 如何在笔记本电脑上全屏显示 QLabel?

标签 c++ qt

<分区>

我有一个带有QButtonQLabelui。我希望每当我按下该按钮时,QLabel 都能在我的笔记本电脑上全屏显示。

我尝试了以下代码:

void MainWindow::on_fullScreenBtn_clicked()
{
    // ui->myImage is a QLabel*
    ui->myImage->setText("going full screen");
    ui->myImage->showMaximized();
    ui->myImage->QWidget::showFullScreen(); 
}

我可以看到这个函数被执行了,但这并没有全屏显示我的标签。我做错了什么/遗漏了什么?

我正在使用 QtCreator 3.5.1 并使用内置的图形界面制作我的 gui。

编辑:变量 myImage 是一个包含图像的 QLabel

最佳答案

问题是您的 QLabel 是另一个小部件的一部分,因此由于默认 Qt::Widget 而显示为子小部件标志:

This is the default type for QWidget. Widgets of this type are child widgets if they have a parent, and independent windows if they have no parent. See also Qt::Window and Qt::SubWindow.

因此,解决方案是手动将标志更改为Qt::Window,如下所示:

ui->myImage->setWindowFlag(Qt::Window);

Qt 5.9 之前的用户可以使用以下内容:

ui->myImage->setWindowFlags(ui->myImage->windowFlags() | Qt::Window);

请注意,在将窗口标志更改为 documented by Qt 之后,您应该调用 showFullScreen :

Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..

撤消最大化

应该删除窗口标志以撤消最大化。这可以按如下方式完成:

ui->myImage->setWindowFlag(Qt::Window, false);
ui->myImage->show();

Qt 5.9 之前的用户可以使用以下内容:

ui->myImage->setWindowFlags(ui->myImage->windowFlags() & ~Qt::Window);
ui->myImage->show();

请注意,如上所述,需要调用 show

关于c++ - Qt - 如何在笔记本电脑上全屏显示 QLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44385978/

相关文章:

qt - PyQt5翻译: is it possible to run pylupdate5 from Python?

c++ - 保存并加载 QList<Class*> 到文件

c++ - MPI_Neighbor_alltoallw() 中发送和接收的正确顺序是什么?

c++ - SetWindowLongPtr 与 DialogBoxParam?

c++ - 为什么赋值运算符会做一些与其匹配的构造函数不同的事情?

c++ - 如何在 C++ 中取消来自客户端的服务器端流式调用?

c++ - QT COMPILER_MACROS 未在项目开始时定义

c++ - QStringList.indexOf() 没有返回正确的值

c++ - QTableView 性能低下,有 1000 个可见单元格

c++ - 数组初始化值太多