Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. confusion using 'this' pointer
QtWS25 Last Chance

confusion using 'this' pointer

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 382 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.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by
    #1

    Hi all,

    I'm opening a new window upon clicking a push button from a QmainWindow as follows:

    void stageProgram::on_pushButton_programKeyGrip_clicked()
    {
       this->hide();
       keygrip = new ProgramKeyGrip(); //---> here 'this' pointer not passed 
       keygrip -> show();
    }
    

    If I dont pass this pointer as above, I can see the Paintevent (drawing two lines in the window) in the ProgramKeyGrip (given below):

    #include "programkeygrip.h"
    #include "ui_programkeygrip.h"
    
    ProgramKeyGrip::ProgramKeyGrip(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::ProgramKeyGrip)
    {
        ui->setupUi(this);
    }
    ProgramKeyGrip::~ProgramKeyGrip()
    {
        delete ui;
    }
    
    void ProgramKeyGrip::paintEvent(QPaintEvent *e)
    {
        QPainter painter1(this); 
    
        QPen linepen1(Qt::darkRed);
        linepen1.setWidth(2);
    
        painter1.setPen(linepen1);
        painter1.drawLine(p11,p12);
        painter1.drawLine(p12,p13);
    }
    
    

    Where as If I do as follows , PaintEvent is not visible in the new window:

    void stageProgram::on_pushButton_programKeyGrip_clicked()
    {
       this->close(); // or hide()
       keygrip = new ProgramKeyGrip(this); //---> here 'this' pointer passed 
       keygrip -> show();
    }
    

    Any idea, what's happening here?

    Thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      In your second case, keygrip becomes a child of the stageProgram and will thus appear in its hierarchy. Since you are closing or hiding its parent, theres no reason for keygrip to be chown.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • V Offline
        V Offline
        viniltc
        wrote on last edited by viniltc
        #3

        @SGaist Thanks for your response.
        My doubt is still- how this affects the paintEvent in the keygrip?

        keygrip = new ProgramKeyGrip(); --->this case paintEvent is visible
        keygrip = new ProgramKeyGrip(this); ----> here paintEvent not visible (all other widgets are visbile)

        JKSHJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

          @viniltc said in confusion using 'this' pointer:

          keygrip = new ProgramKeyGrip(); --->this case paintEvent is visible

          -> not parented so is shown because it ignores the widget were it was created in because it doesn't even know it exists.

          @viniltc said in confusion using 'this' pointer:

          keygrip = new ProgramKeyGrip(this); ----> here paintEvent not visible (all other widgets are visbile)

          Parented to a widget that is hidden so not shown since all the children of said widget will also be hidden.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • V viniltc

            @SGaist Thanks for your response.
            My doubt is still- how this affects the paintEvent in the keygrip?

            keygrip = new ProgramKeyGrip(); --->this case paintEvent is visible
            keygrip = new ProgramKeyGrip(this); ----> here paintEvent not visible (all other widgets are visbile)

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @viniltc said in confusion using 'this' pointer:

            keygrip = new ProgramKeyGrip(this);

            To add to @SGaist, the code above is equivalent to

            keygrip = new ProgramKeyGrip();
            keygrip->setParent(this);
            

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            2
            • V Offline
              V Offline
              viniltc
              wrote on last edited by
              #6

              Thank you @SGaist , @JKSH :)

              1 Reply Last reply
              0

              • Login

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