How to show QGLWidget in full screen? - qt

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.

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_);
}

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.

Related

How can I tell QWidget to show in the same place that it was before it was hidden?

If I create and show a top-level QWidget, drag it to a new position on the desktop, then call widget->hide() followed by widget::show(), it generally reappears in a different place from where it was previously.
I can add code to the subclass which saves and restores its geometry, but I'm wondering if there's an in-Qt system for giving hints to the window manager as to where the widget should appear when shown.
Is there a nice way to do this?
I create a "Qt Widgets Application" and drag in a QPushButton, then add the following code:
void Widget::on_pushButton_clicked()
{
hide();
show();
}
Run the program, drag the widget to a new position, and then click on the button and I find that the widget is still in the same place, not like you said

How to start second QDialog window on side of MainWindow?

I have a application in Qt, and a MainWindow. Now, I also added a helping QDialog which can be hooked up. This QDialog does not influence the programmflow, it just displays information.
But, it always starts on top of the MainWindow :/
SO I would like to start it on the side of the main window, so that it does not hinder the view to the main window?? How?
In my opinion , You should try this,
Use the overload of the QWidget::setParent() function to change the ownership of a QDialog widget, using set as NULL(but It will not share the parent's taskbar entry).
QDialog::show() returns control to the caller immediately, So it will not interfere in mainwindow flow.
Let's say, your application is in the full screen mode. Where then the QDialog should appear? It will be shown on the top and you won't be satisfied again.
If it doesn't influence the flow of the app then you are using it to communicate some sort of message. Can you use different ways? For instance, QStatusBar?
If not, why not to split the mainWindow with QSplitter or similar classes and provide an area where you can post all the info messages?
It sounds you want modaless dialog. In main window, use a slot to create the dialog.
void MainWindow::show_dialog()
{
if ( pDialog== NULL )
pDialog= new XYZ_Dialog(this);
QPoint p = pos();
QSize s = frameSize();
pDialog->setGeometry(p.x()+s.width(), p.y(), s.width()*1/2, s.height());
pDialog->show();
}
What I try to say is if the position of the new dialog bothers you, you set the position and size of it, before showing it.

Is a QGraphicsSceneEvent::widget() always a QGraphicsView?

According to the docs, QGraphicsSceneEvent::widget() returns a QWidget. Isn't the widget always going to be a QGraphicsView, though (or null)?
I would assume so, except then I don't understand why the devs wouldn't have just made it QGraphicsSceneEvent::view().
The reason I'm asking is that in my subclassed QGraphicsScene, I have overridden QGraphicsScene::mousePressEvent() and I want to know which view originated the event--I'm wondering if it's safe to static cast widget() to a QGraphicsView, or if it's conceivable that some other widget could have created the event.
As it turns out, QGraphicsSceneEvent::widget() returns the viewport widget, not the QGraphicsView. If you want the QGraphicsView, you would need to use: event->widget()->parent().
Documentation page you refered mentions the QGraphicsView as a target of events:
When a QGraphicsView receives Qt mouse, keyboard, and drag and drop
events (QMouseEvent, QKeyEvent, QDragEvent, etc.), it translates them
into instances of QGraphicsSceneEvent subclasses and forwards them to
the QGraphicsScene it displays. The scene then forwards the events to
the relevant items.
However, if you doubt that QGraphicsSceneEvent::widget() always returns QGraphicsView, or you relay that it should always be a QGraphicsView you can check that by using qobject_cast:
QGraphicsView *view = qobject_cast<QGraphicsView *>(event->widget();
if (view) {
// Handle the event
} else {
// This is something that I do not expect.
// ..
}

How to detect if focus shift from a widget inside a QWidget to one outside?

I'm programming in Python using Qt with PySide and I have custom QWidget defined in a file called editor.py which is inserted in my ui in windowUi.py using the promotion method in the Qt Designer.
The custom QWidget class defined in editor.py doesn't do much besides using Elixir to edit items in a sqlite3 database and importing a ui file (editor.ui). Inside editor.ui there are a couple of QLineEdits and QDateTime widgets.
This widget is originally hidden in the main window and showed when needed. So far so good, but the problem is that I cannot make it hide when not needed. I decided that the widget is not needed when the user clicks anywhere else in the main window that is not the editor widget imported, that is, focus shift from the QWidget.
I looked upon this question: QWidget focusOutEvent not received and realized that the QWidget is really not getting focus.
If I call setFocusPolicy(StrongFocus) on it then I can make it hide if, and only if, the user clicks on the QWidget background (not on any widget inside it) and then clicks outside.
The question is then, how can I make it such that when the user clicks outside of this widget, shifting focus from any QLineEdit or QDateTime that's inside it to something else, the QWidget then hides itself?
Doesn't QApplication:::focusChanged ( QWidget * old, QWidget * now ) do what you need? You can check if QWidget *now is one of your QLineEdits/QDateTime or not (f.e. by going up by the QObject::parent)
Simply connect to this signal before showing and disconnect after hiding.

How to generate a window (widget) on button press in qt

I have designed a GUI through Qt creator on Linux. This design consists of some fields, text edit and some push buttons.
When I press on the push button I want to display another window. Is there any GUI option for this or any hard code?
You need signals and slots.
You have to connect the clicked signal to a custom slot, created by you, of your main widget.
Corrected Code, based in the comments of Patrice Bernassola and Job.
In the class definition (.h file) add the lines:
Q_OBJECT
private slots:
void exampleButtonClicked();
private:
QDialog *exampleDialog;
The macro Q_OBJECT is needed when you define signals or slots in your classes.
The variable exampleDialog should be declared in the definition file to have access to it in the slot.
And you have to initialize it, this is commonly done in the constructor
ExampleClass::ExampleClass()
{
//Setup you UI
dialog = new QDialog;
}
In the class implementation (.cpp file) add the code that does what you want, in this case create a new window.
void ExampleClass::exampleButtonClicked()
{
exampleDialog->show();
}
And also you have to connect the signal to the slot with the line:
connect(exampleButton, SIGNAL(clicked()), this, SLOT(exampleButtonClicked()));
Your question is somehwat basic, so I suggest to read a basic tutorial, in this way you can make progress faster, avoiding waiting for answers.
Some links to tutorials that were useful to me:
http://zetcode.com/tutorials/qt4tutorial/
http://doc.qt.io/archives/qt-4.7/tutorials-addressbook.html
on click event of button you create another widget and show.
another option is Stacked widget, http://doc.trolltech.com/4.6/qstackedwidget.html

Resources