"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?
-
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?
thisis passed to setthis(theQWidgetyou 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
QMessageBoxin relation to yourQWidget, that invokes it instead of (parentless) "floating around".
(EDIT: Instead of passing a valid widget, anullptris 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 standardQMessageBoxdoes not. -
It's possible to pass
nullptras a parent.
The parent here acts only to position the message box relatively to it (on top of it, in the same window). -
It's possible to pass
nullptras 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 hasnullptras 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. -
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?
@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