Unsolved "this" keyword passed as parent parameter
-
QMessageBox::information(this,"Message","You clicked on that button",QMessageBox::Ok);
I know that the "this" keyword returns the address of the current object, but I didn't get it in the context above.
What does the "this" keyword really do in the context above and why it's provided it as its parent?
-
this
is passed to setthis
(theQWidget
you are currently in) as parent of yourQMessageBox
.
If you leave it out, which is not possible in your case, your standardQMessageBox
' parent is a nullptr (= no parent).It's necessary here because it shows this special type of
QMessageBox
in relation to yourQWidget
, that invokes it instead of (parentless) "floating around".
(EDIT: Instead of passing a valid widget, anullptr
is possible too, like @GrecKo said below, but you can't leave it out)So these static MessageBoxes likeQMessageBox::information()
orQMessageBox::warning()
need a parent, while a standardQMessageBox
does not. -
It's possible to pass
nullptr
as a parent.
The parent here acts only to position the message box relatively to it (on top of it, in the same window). -
But why the QMessageBox standard constructor hasnullptr
as default value while the static messageBox functions don't?
(Edit: Never mind... I didn't realized the parameter order)Of course you can pass a
nullptr
, but in your case, you can not just leave it out. -
@cs_student said in "this" keyword passed as parent parameter:
What does the "this" keyword really do in the context above and why it's provided it as its parent?
Read this: https://doc.qt.io/qt-5/objecttrees.html