Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between hide, close and show in qt

Tags:

qt

qt5

What is the difference between hide,close and show of pushbutton or any widget in terms of memory?

Which is better if I don't want to use widget again?

like image 644
Mitesh Patel Avatar asked Sep 09 '16 08:09

Mitesh Patel


People also ask

How to close widget in Qt?

The promoted widget has a number of labels and buttons. One of the buttons is a home button. On clicking this button the user needs to close the form and open the home form.

How do I hide mainWindow in Qt?

Then in mainWindow I hide it and display dialog window with this code: this->hide(); SomeDialog x; x. setModal(true); x. exec();

How to close window in Qt c++?

UNSOLVED How To Close All Window To quit, you can use void QCoreApplication::quit() [static slot] , see http://doc.qt.io/Qt-5/qcoreapplication.html#quit.

How do I hide layout in Qt?

A layout can not be hidden since they are not widgets and does not have a setVisible() function. You have to create a widget that you can hide, that contains the layout and the other widgets that should be hidden, then you hide the widget instead of the layout.


2 Answers

First as said @Hayt, read the documentation.

For the actual answer:

  1. hide() is the same as setVisible(false).
  2. show() is the same as setVisible(true).
  3. close() attempts to close the widget by triggering a QCloseEvent, if the event is accepted the result is:

    1. The same as calling hide() if Qt::WA_DeleteOnClose attribute is not set on the widget which is the default.

    2. The same as calling deleteLater() if Qt::WA_DeleteOnClose is set.

In term of memory, any of the 3 will not change anything (except for close() if you have set Qt::WA_DeleteOnClose). If you do not want to use the widget ever, the best is to delete it:

delete pointerToMyWidget;

or

pointerToMyWidget->deleteLater();

The second form is generally safer as the 1st one can be dangerous depending on where your write it. (e.g you delete it in a slot called by a signal emitted by the widget you delete).

like image 85
Benjamin T Avatar answered Oct 21 '22 07:10

Benjamin T


According to Qt, you can read this :

CLOSE : Closes this widget. Returns true if the widget was closed; otherwise returns false.

First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event.

If the widget has the Qt::WA_DeleteOnClose flag, the widget is also deleted. A close events is delivered to the widget no matter if the widget is visible or not.

The QApplication::lastWindowClosed() signal is emitted when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus.

.

HIDE : Hides the widget. This function is equivalent to setVisible(false).

Note: If you are working with QDialog or its subclasses and you invoke the show() function after this function, the dialog will be displayed in its original position.

.

SHOW : Shows the widget and its child widgets. This function is equivalent to setVisible(true).

If you don't need to use your widget, call close(). You can manage the event to destroy your widget.

hide() only hides. It's only graphical, you can't see your widget but you don't destroy it.

But I think that the name fo the function are enough explicit to understand!

like image 44
Landelin Delcoucq Avatar answered Oct 21 '22 06:10

Landelin Delcoucq