How to show QGLWidget in full screen?

2019-08-11 02:04发布

问题:

I have a QGLWidget as part of the UI of my application. It is NOT a central widget, there are a lot of others widgets around it. I want to show it full screen on user clicks the button. Similar functionality like on youtube video flash player.

I have tried to use showFullScreen with no effect.

I have read how-to-fullscreen-a-qglwidget and fullscreen-widget, but they suggest using showFullScreen.

Qt documentation states that for using showFullScreen widget must be an independent window. So I assume there should be some trick for this.

回答1:

The solution I found:

void MyApp::on_fullscreen_button_clicked() {
    QDialog *dlg = new QDialog(this);
    QHBoxLayout *dlg_layout = new QHBoxLayout(dlg);
    dlg_layout->setContentsMargins(0, 0, 0, 0);
    dlg_layout->addWidget(glwidget_);
    dlg->setLayout(dlg_layout);
    dlg->showFullScreen();

    bool r = connect(dlg, SIGNAL(rejected()), this, SLOT(showGlNormal()));
    assert(r);
    r = connect(dlg, SIGNAL(accepted()), this, SLOT(showGlNormal()));
    assert(r);
}

void MyApp::showGlNormal() {
    ui.glBox->layout()->addWidget(glwidget_);
}


回答2:

The showFullScreen function works only on windows. From the Qt documentation:

A window is a widget that isn't visually the child of any other widget and that usually has a frame and a window title.

A possible solution is the following:

When the user clicks the show full screen button

  • Create a new QGlWidget with no parent and set to it the contents of you QGlWidget
  • Use the showFullScreen function on it...

Maybe it is a better idea to subclass QGlWidget and pass in its constructor a pointer to another QGlWidget. The constructor should take the context of the provided widget and apply it to the new one.

  • On your subclass catch keyboard events. When the user press Esc emit a signal
  • In your base class catch this signal and connect it to a slot. In this slot hide the full screen QGlWidget and delete it.