Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. "this" keyword passed as parent parameter

"this" keyword passed as parent parameter

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 525 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cs_student
    wrote on last edited by
    #1

    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?

    Pl45m4P jsulmJ 2 Replies Last reply
    0
    • C cs_student

      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?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @cs_student

      this is passed to set this (the QWidget you are currently in) as parent of your QMessageBox.
      If you leave it out, which is not possible in your case, your standard QMessageBox ' parent is a nullptr (= no parent).

      It's necessary here because it shows this special type of QMessageBox in relation to your QWidget, that invokes it instead of (parentless) "floating around".
      (EDIT: Instead of passing a valid widget, a nullptr is possible too, like @GrecKo said below, but you can't leave it out)

      So these static MessageBoxes like QMessageBox::information() or QMessageBox::warning() need a parent, while a standard QMessageBox does not.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        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).

        Pl45m4P 1 Reply Last reply
        4
        • GrecKoG GrecKo

          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).

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @GrecKo

          But why the QMessageBox standard constructor has nullptr 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.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • C cs_student

            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?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved