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?
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.
Then in mainWindow I hide it and display dialog window with this code: this->hide(); SomeDialog x; x. setModal(true); x. exec();
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.
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.
First as said @Hayt, read the documentation.
For the actual answer:
hide()
is the same as setVisible(false)
.show()
is the same as setVisible(true)
.close()
attempts to close the widget by triggering a QCloseEvent
, if the event is accepted the result is:
The same as calling hide()
if Qt::WA_DeleteOnClose
attribute is not set on the widget which is the default.
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With