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. Analysing simple codesnips
Forum Updated to NodeBB v4.3 + New Features

Analysing simple codesnips

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 3 Posters 3.9k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    tomy
    wrote on 8 Jan 2019, 17:10 last edited by
    #1

    Hello all,

    There are simple codesnips from the CustomWidget example of the QML online book, here:

    void CustomWidget::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        QRect r1 = rect().adjusted(10, 10, -10, -10);
        painter.setPen(QColor("#33B5E5"));
        painter.drawRect(r1);
    
        QRect r2(QPoint(0, 0), QSize(40, 40));
    
        if(m_lastPos.isNull())
            r2.moveCenter(r1.center()); 
        else
            r2.moveCenter(m_lastPos);
    
        painter.fillRect(r2, QColor("#FFBB33"));
    }
    
    int main(int argc, char** argv)
    {
        QApplication app(argc, argv);
        QScopedPointer<QWidget> widget(new CustomWidget());
           //CustomWidget* widget = new CustomWidget(); 
        widget -> resize(600, 400);
        widget -> show();
    
        return app.exec();
    }
    

    Are my understandings correct:

    • paintEvent is an overwritten method here for repainting part or all of its widget which is initially left blank

    • QPainter acts as a brush to to draw/fill something

    • rect().adjusted adds a new rectangle on (?)

    • I imagine QWidget is a rectangle we using resize make is smaller.

    • I think the commented line in main works just like QScopedPointer and these two have no difference (at least here)

    • In painEvent if we move this statement painter.fillRect(r2, QColor("#FFBB33")); up before if, the program's behaviour will astonishingly be completely different! (Why)
      Its duty is only painting the smaller rectangle.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 8 Jan 2019, 17:50 last edited by dheerendra 1 Sept 2019, 01:57
      #2
      1. Entire widget area.
      2. Qpainter acts as drawing tool. Refer qpainetr class for more information
      3. It does not add one more rect.
      4. yes
      5. Difference is w.r.tto scope of variable and what shud be done with object when no scope. Commented code does not delete object
      6. What is getting drawn first matters.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      T 1 Reply Last reply 9 Jan 2019, 14:35
      5
      • D dheerendra
        8 Jan 2019, 17:50
        1. Entire widget area.
        2. Qpainter acts as drawing tool. Refer qpainetr class for more information
        3. It does not add one more rect.
        4. yes
        5. Difference is w.r.tto scope of variable and what shud be done with object when no scope. Commented code does not delete object
        6. What is getting drawn first matters.
        T Offline
        T Offline
        tomy
        wrote on 9 Jan 2019, 14:35 last edited by tomy 1 Sept 2019, 14:47
        #3

        @dheerendra
        Thanks.

        1. Entire widget area.

        This event handler can be reimplemented in a subclass to receive paint events passed in event.
        A paint event is a request to repaint all or part of a widget.

        By the way, in void CustomWidget::paintEvent(QPaintEvent *) paintEvent is painting one of the two rectangles.

        1. It does not add one more rect.

        It says: Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of this rectangle.

        1. yes

        Does it mean that QWidget is always defined as a rectangle of specific width and height? Then we can make it smaller/begger using resize, if needed. Yeah?

        1. Difference is w.r.tto scope of variable and what shud be done with object when no scope. Commented code does not delete object
        2. What is getting drawn first matters.

        w.r.tto?
        Where will the object (window) be deleted if we use QScopedPointer?
        Incidentally, the behaviour with both is the same when testing the program.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 9 Jan 2019, 17:01 last edited by
          #4

          I'm not very clear for which one I need to answer. Let me try to answer.

          1. QWidget is not a rectangle. It is visual elements whose size is defined with rectangle.
          2. Since you are writing the code in main.cpp you don't see much difference. Use some method and use QScopedPointer. You will see the difference.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          T 1 Reply Last reply 9 Jan 2019, 20:11
          0
          • D dheerendra
            9 Jan 2019, 17:01

            I'm not very clear for which one I need to answer. Let me try to answer.

            1. QWidget is not a rectangle. It is visual elements whose size is defined with rectangle.
            2. Since you are writing the code in main.cpp you don't see much difference. Use some method and use QScopedPointer. You will see the difference.
            T Offline
            T Offline
            tomy
            wrote on 9 Jan 2019, 20:11 last edited by
            #5

            @dheerendra
            The first two texts in italic are quotes from Docs which seemingly are unlike what you said.
            The next two ones were questions that you sent replies as answers.
            Thanks. But I haven't understood that simple code completely yet.

            M 1 Reply Last reply 9 Jan 2019, 20:25
            0
            • T tomy
              9 Jan 2019, 20:11

              @dheerendra
              The first two texts in italic are quotes from Docs which seemingly are unlike what you said.
              The next two ones were questions that you sent replies as answers.
              Thanks. But I haven't understood that simple code completely yet.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Jan 2019, 20:25 last edited by SGaist 1 Sept 2019, 20:43
              #6

              Hi
              Lets first talk about QScopedPointer.
              its used to auto delete a class (one has newed) automatically as soon as the QScopedPointer
              runs out of scope. Scope is often the function its located in. So scope ends at last }
              However, this example is not really good as there is no need to actually new the widget since
              app.exec(); is blocking and first ends when app ends.

              Also, often a widget is assigned a parent which will then delete it when parent is deleted. (ownership).

              Also, if you did this in a function, you would never see the actual widget as it would be deleted as soon as function ends.
              So QScopedPointer is more often used with own data class that is not widgets.
              It would make more sense to do
              int main(int argc, char** argv)
              {
              QApplication app(argc, argv);
              CustomWidget widget;
              widget.resize(600, 400);
              widget.show();
              return app.exec();
              }

              For your other questions.

              • paintEvent is an overwritten method here for repainting part or all of its widget which is initially left blank
                This virtual function is called by the framework and its expected to paint the Widget in its current state.
                For QWidget is not much code, but say for QPushButton it has code to draw the button.

              • QPainter acts as a brush to to draw/fill something
                Its the class that can do drawing onto the screen. It contains tons of function to draw
                various shapes on screen.

              • rect().adjusted adds a new rectangle on (?)
                Yes it returns a new QRect by copy with its value adjusted.
                Note, there is also Adjust function which does alter the QRect in place.

              • I imagine QWidget is a rectangle we using resize make is smaller.
                The Geometry of any widget is a rectangle. While you can use a mask to paint say a round
                window, any hit testing, like mouse clicks still operates on the rectangular definition.
                So yes, you can say all widgets are rectangular.

              • In painEvent if we move this statement painter.fillRect(r2, QColor("#FFBB33")); up before if, the program's behaviour will astonishingly be completely different! (Why)

              well the if will alter r2 always.
              if you move before if, its will draw a rect that is 10 less on all sides that full rect.
              the if will move the rect to center around a point. that part is missing if moved before if.

              [edit: fixed typo code SGaist]

              T 1 Reply Last reply 10 Jan 2019, 13:32
              6
              • M mrjj
                9 Jan 2019, 20:25

                Hi
                Lets first talk about QScopedPointer.
                its used to auto delete a class (one has newed) automatically as soon as the QScopedPointer
                runs out of scope. Scope is often the function its located in. So scope ends at last }
                However, this example is not really good as there is no need to actually new the widget since
                app.exec(); is blocking and first ends when app ends.

                Also, often a widget is assigned a parent which will then delete it when parent is deleted. (ownership).

                Also, if you did this in a function, you would never see the actual widget as it would be deleted as soon as function ends.
                So QScopedPointer is more often used with own data class that is not widgets.
                It would make more sense to do
                int main(int argc, char** argv)
                {
                QApplication app(argc, argv);
                CustomWidget widget;
                widget.resize(600, 400);
                widget.show();
                return app.exec();
                }

                For your other questions.

                • paintEvent is an overwritten method here for repainting part or all of its widget which is initially left blank
                  This virtual function is called by the framework and its expected to paint the Widget in its current state.
                  For QWidget is not much code, but say for QPushButton it has code to draw the button.

                • QPainter acts as a brush to to draw/fill something
                  Its the class that can do drawing onto the screen. It contains tons of function to draw
                  various shapes on screen.

                • rect().adjusted adds a new rectangle on (?)
                  Yes it returns a new QRect by copy with its value adjusted.
                  Note, there is also Adjust function which does alter the QRect in place.

                • I imagine QWidget is a rectangle we using resize make is smaller.
                  The Geometry of any widget is a rectangle. While you can use a mask to paint say a round
                  window, any hit testing, like mouse clicks still operates on the rectangular definition.
                  So yes, you can say all widgets are rectangular.

                • In painEvent if we move this statement painter.fillRect(r2, QColor("#FFBB33")); up before if, the program's behaviour will astonishingly be completely different! (Why)

                well the if will alter r2 always.
                if you move before if, its will draw a rect that is 10 less on all sides that full rect.
                the if will move the rect to center around a point. that part is missing if moved before if.

                [edit: fixed typo code SGaist]

                T Offline
                T Offline
                tomy
                wrote on 10 Jan 2019, 13:32 last edited by tomy 1 Oct 2019, 13:32
                #7

                @mrjj

                Hi
                Lets first talk about QScopedPointer.
                its used to auto delete a class (one has newed) automatically as soon as the QScopedPointer
                runs out of scope. Scope is often the function its located in. So scope ends at last }
                However, this example is not really good as there is no need to actually new the widget since
                app.exec(); is blocking and first ends when app ends.

                Also, often a widget is assigned a parent which will then delete it when parent is deleted. (ownership).

                Also, if you did this in a function, you would never see the actual widget as it would be deleted as soon as function ends.
                So QScopedPointer is more often used with own data class that is not widgets.
                It would make more sense to do
                int main(int argc, char** argv)
                {
                QApplication app(argc, argv);
                CustomWidget widget;
                widget.resize(600, 400);
                widget.show();
                return app.exec();
                }

                Hi,

                I, too, thought that way and it was the real rationale I asked that question about QScopePointer because it has no difference with that description you described to the commented replacement.

                This virtual function is called by the framework and its expected to paint the Widget in its current state.
                For QWidget is not much code, but say for QPushButton it has code to draw the button.

                Now what is/are its painting(s) in the code? I know rect2 is filled but rect1 is only outlined. And we didn't need to use that virtual function, we could use something like below:

                void CustomWidget::painting() {
                ...
                }
                

                Because it's QPainter which does paint.

                Its the class that can do drawing onto the screen. It contains tons of function to draw
                various shapes on screen.

                Yeah, thanks.
                I also found something else in mind. Can it draw a circle a paint it?

                Yes it returns a new QRect by copy with its value adjusted.

                What do you mean by that, please? For example, I think it creates a new over a prior rect. But where is the prior one?

                well the if will alter r2 always.
                if you move before if, its will draw a rect that is 10 less on all sides that full rect.
                the if will move the rect to center around a point. that part is missing if moved before if.

                I think you misunderstood my talk. I meant this:

                ...
                    QRect r2(QPoint(0, 0), QSize(40, 40));
                    painter.fillRect(r2, QColor("#FFBB33"));
                
                    if(m_lastPos.isNull())
                        r2.moveCenter(r1.center()); 
                    else
                        r2.moveCenter(m_lastPos);    
                }
                

                painter.fillRect(r2, QColor("#FFBB33"));'s act is only painting rect2; it musn't have anything to do with moveCenter.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 10 Jan 2019, 13:42 last edited by
                  #8

                  Hi
                  You are only allowed to use QPainter in the paintEvent function.
                  You cannot use other name as Qt would not call your paint function and nothing would be drawn.
                  you could do

                  
                  void CustomWidget::paintEvent(QPaintEvent *)
                  {
                    QPainter p(this); 
                    painting(&p);
                  

                  but
                  void CustomWidget::painting() alone would not work.

                  and yes it can draw a circle
                  painter.drawEllipse(QPointF(x,y), radius, radius);
                  and fill it with QBrush.

                  • I think it creates a new over a prior rect. But where is the prior one?
                    QRect r1 = rect().adjusted(10, 10, -10, -10);
                    the rect function returns a QRect and calling adjusted returns a new one with altered values.
                    so the prior one is rect();

                  When you do like this

                   QRect r2(QPoint(0, 0), QSize(40, 40));
                      painter.fillRect(r2, QColor("#FFBB33"));
                  
                      if(m_lastPos.isNull())
                          r2.moveCenter(r1.center()); 
                      else
                          r2.moveCenter(m_lastPos);    
                  }
                  

                  you paint the rect BEFORE its alterd. the next moveCenter have no effect as its already painted.
                  Hence it looks different as r2 is no longer changed.

                  T 1 Reply Last reply 10 Jan 2019, 15:06
                  5
                  • M mrjj
                    10 Jan 2019, 13:42

                    Hi
                    You are only allowed to use QPainter in the paintEvent function.
                    You cannot use other name as Qt would not call your paint function and nothing would be drawn.
                    you could do

                    
                    void CustomWidget::paintEvent(QPaintEvent *)
                    {
                      QPainter p(this); 
                      painting(&p);
                    

                    but
                    void CustomWidget::painting() alone would not work.

                    and yes it can draw a circle
                    painter.drawEllipse(QPointF(x,y), radius, radius);
                    and fill it with QBrush.

                    • I think it creates a new over a prior rect. But where is the prior one?
                      QRect r1 = rect().adjusted(10, 10, -10, -10);
                      the rect function returns a QRect and calling adjusted returns a new one with altered values.
                      so the prior one is rect();

                    When you do like this

                     QRect r2(QPoint(0, 0), QSize(40, 40));
                        painter.fillRect(r2, QColor("#FFBB33"));
                    
                        if(m_lastPos.isNull())
                            r2.moveCenter(r1.center()); 
                        else
                            r2.moveCenter(m_lastPos);    
                    }
                    

                    you paint the rect BEFORE its alterd. the next moveCenter have no effect as its already painted.
                    Hence it looks different as r2 is no longer changed.

                    T Offline
                    T Offline
                    tomy
                    wrote on 10 Jan 2019, 15:06 last edited by tomy 1 Oct 2019, 15:07
                    #9

                    @mrjj Hi, and thanks.

                    Qt would not call your paint function

                    Does it mean that those three virtual functions in the code are automatically called by the event loop? It might be true and I just realized that because there is no statement in the code to call them! (We just create an object of the class)

                    QRect r1 = rect().adjusted(10, 10, -10, -10);
                    the rect function returns a QRect and calling adjusted returns a new one with altered values.
                    so the prior one is rect();

                    So, there's a rectangle created by rect() with a reasonable size, then using adjusted we narrow it by 10 points from both up-left and down-right corners. Is it right?

                    If right, it sounds redundant, because we could create a rect with the desired size without need to adjusting that. Couldn't we?

                    ou paint the rect BEFORE its alterd

                    My problem is with that "altered", I say it's moved and it's not changed/altered.
                    For example, you take a white vase to the living room then there paint it. Another day, you paint another white vase and take it to the living room.

                    M 1 Reply Last reply 10 Jan 2019, 15:22
                    0
                    • T tomy
                      10 Jan 2019, 15:06

                      @mrjj Hi, and thanks.

                      Qt would not call your paint function

                      Does it mean that those three virtual functions in the code are automatically called by the event loop? It might be true and I just realized that because there is no statement in the code to call them! (We just create an object of the class)

                      QRect r1 = rect().adjusted(10, 10, -10, -10);
                      the rect function returns a QRect and calling adjusted returns a new one with altered values.
                      so the prior one is rect();

                      So, there's a rectangle created by rect() with a reasonable size, then using adjusted we narrow it by 10 points from both up-left and down-right corners. Is it right?

                      If right, it sounds redundant, because we could create a rect with the desired size without need to adjusting that. Couldn't we?

                      ou paint the rect BEFORE its alterd

                      My problem is with that "altered", I say it's moved and it's not changed/altered.
                      For example, you take a white vase to the living room then there paint it. Another day, you paint another white vase and take it to the living room.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 10 Jan 2019, 15:22 last edited by mrjj 1 Oct 2019, 15:23
                      #10

                      Hi

                      • Does it mean that those three virtual functions in the code are automatically called by the event loop? It might be true and I just realized that > because there is no statement in the code to call them! (We just create an object of the class)

                      yes the paintEvent ( among many others) are called by the Qt framework and its part of the system that makes it all work.

                      • So, there's a rectangle created by rect() with a reasonable size, then using adjusted we narrow it by 10 points from both up-left and down-right corners. Is it right? If right, it sounds redundant, because we could create a rect with the desired size without need to adjusting that. Couldn't we?

                      Yes you could also create the r1 rect without using "adjusted" but would be more code for same result if you want to
                      have a rect that is 10 less that whole widgets rect. but you can set the x,y and width, height of a QRect in many ways.
                      This is just one way.

                      • My problem is with that "altered", I say it's moved and it's not changed/altered.
                        In any case the the x,y of the r2 is altered and hence its painted differently.
                        Its centers around a point. if painted before moveCenter. its not centered.
                      T 1 Reply Last reply 10 Jan 2019, 17:28
                      2
                      • M mrjj
                        10 Jan 2019, 15:22

                        Hi

                        • Does it mean that those three virtual functions in the code are automatically called by the event loop? It might be true and I just realized that > because there is no statement in the code to call them! (We just create an object of the class)

                        yes the paintEvent ( among many others) are called by the Qt framework and its part of the system that makes it all work.

                        • So, there's a rectangle created by rect() with a reasonable size, then using adjusted we narrow it by 10 points from both up-left and down-right corners. Is it right? If right, it sounds redundant, because we could create a rect with the desired size without need to adjusting that. Couldn't we?

                        Yes you could also create the r1 rect without using "adjusted" but would be more code for same result if you want to
                        have a rect that is 10 less that whole widgets rect. but you can set the x,y and width, height of a QRect in many ways.
                        This is just one way.

                        • My problem is with that "altered", I say it's moved and it's not changed/altered.
                          In any case the the x,y of the r2 is altered and hence its painted differently.
                          Its centers around a point. if painted before moveCenter. its not centered.
                        T Offline
                        T Offline
                        tomy
                        wrote on 10 Jan 2019, 17:28 last edited by
                        #11

                        @mrjj Hi and thanks so much for your help.

                        ( among many others)

                        So here in this code, only paintEvent is called by Qt automatically, and not other two. mousePressevent/mouseMoveevent is invoked when the mouse is pressed/moved. Yeah?

                        be more code for same result if you want to
                        have a rect that is 10 less that whole widgets rect

                        So the rect() function here, knows the size of the application/widget's window and creates a new but rather smaller one over that. Right?

                        In any case the the x,y of the r2 is altered and hence its painted differently.
                        Its centers around a point. if painted before moveCenter. its not centered.

                        You say that when the coordinates of r2 are changed it's just as it itself is changed, hence it's altered. OK I got it. But why if it's painted before moveCenter it's not centered? Why should a movement task has that thing to do with the color of an object to act that differently?

                        M 1 Reply Last reply 10 Jan 2019, 17:52
                        0
                        • T tomy
                          10 Jan 2019, 17:28

                          @mrjj Hi and thanks so much for your help.

                          ( among many others)

                          So here in this code, only paintEvent is called by Qt automatically, and not other two. mousePressevent/mouseMoveevent is invoked when the mouse is pressed/moved. Yeah?

                          be more code for same result if you want to
                          have a rect that is 10 less that whole widgets rect

                          So the rect() function here, knows the size of the application/widget's window and creates a new but rather smaller one over that. Right?

                          In any case the the x,y of the r2 is altered and hence its painted differently.
                          Its centers around a point. if painted before moveCenter. its not centered.

                          You say that when the coordinates of r2 are changed it's just as it itself is changed, hence it's altered. OK I got it. But why if it's painted before moveCenter it's not centered? Why should a movement task has that thing to do with the color of an object to act that differently?

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 10 Jan 2019, 17:52 last edited by mrjj 1 Oct 2019, 17:56
                          #12

                          Hi

                          • So here in this code, only paintEvent is called by Qt automatically, and not other two. mousePressevent/mouseMoveevent is invoked when - the mouse is pressed/moved. Yeah?

                          Well Qt calls paintEvent when a widgets needs to be painted. Qt calls the (also virtual ) function for mouse events if
                          a mouse event come from the OS and the mouse cursor is over the widget.

                          • So the rect() function here, knows the size of the application/widget's window and creates a new but rather smaller one over that. Right?
                            the rect() function returns a widgets area as a QRect. and adjusted will then read values and return a new QRect. (by copy)

                          • You say that when the coordinates of r2 are changed it's just as it itself is changed, hence it's altered. OK I got it. But why if it's painted before -moveCenter it's not centered? Why should a movement task has that thing to do with the color of an object to act that differently?
                            before moveCenter, its values are 0,0 to 40,40. so if while widget rect() is 0,0, 500,500 , it would paint up in the corner.
                            After move center its x,y is different. ( depending on the point its been centered around)
                            it would alter the x,y of the Rect so the point given would be center of the rect. and hence, its positions(x,y) are changed.

                          T 1 Reply Last reply 10 Jan 2019, 18:34
                          1
                          • M mrjj
                            10 Jan 2019, 17:52

                            Hi

                            • So here in this code, only paintEvent is called by Qt automatically, and not other two. mousePressevent/mouseMoveevent is invoked when - the mouse is pressed/moved. Yeah?

                            Well Qt calls paintEvent when a widgets needs to be painted. Qt calls the (also virtual ) function for mouse events if
                            a mouse event come from the OS and the mouse cursor is over the widget.

                            • So the rect() function here, knows the size of the application/widget's window and creates a new but rather smaller one over that. Right?
                              the rect() function returns a widgets area as a QRect. and adjusted will then read values and return a new QRect. (by copy)

                            • You say that when the coordinates of r2 are changed it's just as it itself is changed, hence it's altered. OK I got it. But why if it's painted before -moveCenter it's not centered? Why should a movement task has that thing to do with the color of an object to act that differently?
                              before moveCenter, its values are 0,0 to 40,40. so if while widget rect() is 0,0, 500,500 , it would paint up in the corner.
                              After move center its x,y is different. ( depending on the point its been centered around)
                              it would alter the x,y of the Rect so the point given would be center of the rect. and hence, its positions(x,y) are changed.

                            T Offline
                            T Offline
                            tomy
                            wrote on 10 Jan 2019, 18:34 last edited by tomy 1 Oct 2019, 18:35
                            #13

                            @mrjj

                            it would alter the x,y of the Rect so the point given would be center of the rect. and hence, its positions(x,y) are changed.

                            I still can't comprehend your perspective!
                            We have a rectangle named r2. Its position firstly is (0,0), (40,40). The function painter.fillRect(r2, QColor("#FFBB33")); fills r2 using its name not its new position. So it's firstly one the top-left corner and painted, then it goes somewhere else on the area. So as an independent widget, it must keep its colour wherever it is.

                            M 1 Reply Last reply 10 Jan 2019, 18:46
                            0
                            • T tomy
                              10 Jan 2019, 18:34

                              @mrjj

                              it would alter the x,y of the Rect so the point given would be center of the rect. and hence, its positions(x,y) are changed.

                              I still can't comprehend your perspective!
                              We have a rectangle named r2. Its position firstly is (0,0), (40,40). The function painter.fillRect(r2, QColor("#FFBB33")); fills r2 using its name not its new position. So it's firstly one the top-left corner and painted, then it goes somewhere else on the area. So as an independent widget, it must keep its colour wherever it is.

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 10 Jan 2019, 18:46 last edited by mrjj 1 Oct 2019, 18:49
                              #14

                              @tomy
                              well
                              painter.fillRect(r2, QColor("#FFBB33"));
                              would indeed be same as
                              painter.fillRect(QRect(0,0,40,40), QColor("#FFBB33"));
                              and would paint in corner. and never move.

                              however, if you modify the r2 BEFORE painting with it, (with r2.moveCenter(r1.center()); )
                              the r2 rect would be changed. and paint in other location.

                              But im not sure what you doubts still are ?

                              if you omit moveCenter, r2 is unchanged.
                              If you apply moveCenter, its changed.
                              if you paint with r2, BEFORE you change it,
                              nothing will happen with moveCenter as the fillRect was already run.

                              T 1 Reply Last reply 10 Jan 2019, 20:26
                              1
                              • M mrjj
                                10 Jan 2019, 18:46

                                @tomy
                                well
                                painter.fillRect(r2, QColor("#FFBB33"));
                                would indeed be same as
                                painter.fillRect(QRect(0,0,40,40), QColor("#FFBB33"));
                                and would paint in corner. and never move.

                                however, if you modify the r2 BEFORE painting with it, (with r2.moveCenter(r1.center()); )
                                the r2 rect would be changed. and paint in other location.

                                But im not sure what you doubts still are ?

                                if you omit moveCenter, r2 is unchanged.
                                If you apply moveCenter, its changed.
                                if you paint with r2, BEFORE you change it,
                                nothing will happen with moveCenter as the fillRect was already run.

                                T Offline
                                T Offline
                                tomy
                                wrote on 10 Jan 2019, 20:26 last edited by
                                #15

                                @mrjj
                                Got it, thank you.

                                Each time the mouse is clicked, all the statements of the paintEvent method are run, while only the if condition and painter are needed to be re-run each time.
                                The code looks raw or, as Stroustrup says, ugly. Is it not?

                                M 1 Reply Last reply 10 Jan 2019, 21:22
                                0
                                • T tomy
                                  10 Jan 2019, 20:26

                                  @mrjj
                                  Got it, thank you.

                                  Each time the mouse is clicked, all the statements of the paintEvent method are run, while only the if condition and painter are needed to be re-run each time.
                                  The code looks raw or, as Stroustrup says, ugly. Is it not?

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 10 Jan 2019, 21:22 last edited by
                                  #16

                                  @tomy
                                  Hi
                                  Well its not for sure a mousePress event will cause a widget to repaint.
                                  But for say for a QPushButton it will so button can draw as pressed.
                                  Its pretty basic painting with one color rectangle.
                                  Much widgets have much more complicated paint code.

                                  T 1 Reply Last reply 10 Jan 2019, 21:59
                                  0
                                  • M mrjj
                                    10 Jan 2019, 21:22

                                    @tomy
                                    Hi
                                    Well its not for sure a mousePress event will cause a widget to repaint.
                                    But for say for a QPushButton it will so button can draw as pressed.
                                    Its pretty basic painting with one color rectangle.
                                    Much widgets have much more complicated paint code.

                                    T Offline
                                    T Offline
                                    tomy
                                    wrote on 10 Jan 2019, 21:59 last edited by
                                    #17

                                    @mrjj

                                    Well its not for sure a mousePress event will cause a widget to repaint.

                                    For example, we click on different areas of the widget for five times and the r2 rectangle traces us, how many times will the method paintEvent be called?

                                    If five times, so in each call, all the statements inside the method are called. Isn't it?

                                    M 1 Reply Last reply 11 Jan 2019, 07:28
                                    0
                                    • T tomy
                                      10 Jan 2019, 21:59

                                      @mrjj

                                      Well its not for sure a mousePress event will cause a widget to repaint.

                                      For example, we click on different areas of the widget for five times and the r2 rectangle traces us, how many times will the method paintEvent be called?

                                      If five times, so in each call, all the statements inside the method are called. Isn't it?

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 11 Jan 2019, 07:28 last edited by
                                      #18

                                      @tomy
                                      Hi
                                      if the r2 follows it does mean for each click, the paint is called.
                                      i assume there is an update() in mousePressEvent/release.
                                      yes all code in painteEvent is run each time.

                                      T 1 Reply Last reply 11 Jan 2019, 09:19
                                      0
                                      • M mrjj
                                        11 Jan 2019, 07:28

                                        @tomy
                                        Hi
                                        if the r2 follows it does mean for each click, the paint is called.
                                        i assume there is an update() in mousePressEvent/release.
                                        yes all code in painteEvent is run each time.

                                        T Offline
                                        T Offline
                                        tomy
                                        wrote on 11 Jan 2019, 09:19 last edited by
                                        #19

                                        @mrjj
                                        Hi, good morning. :)

                                        Yes, r2 follows the click presses and there's an update() which makes it happen by re-calling painEvent each time.
                                        My assumption was that, the part of paintEvent below is useful only for the first time when the program runs.

                                        QPainter painter(this);
                                           QRect r1 = rect().adjusted(10, 10, -10, -10);
                                           painter.setPen(QColor("#33B5E5"));
                                           painter.drawRect(r1);
                                           QRect r2(QPoint(0, 0), QSize(40, 40));
                                        

                                        Afterwards when the user clicks different locations on the widget, only the following section is needed and the above one will be excessive.

                                        if(m_lastPos.isNull())
                                                r2.moveCenter(r1.center()); 
                                            else
                                                r2.moveCenter(m_lastPos);
                                              painter.fillRect(r2, QColor("#FFBB33"));
                                        }
                                        

                                        Disagree?

                                        M 1 Reply Last reply 11 Jan 2019, 10:29
                                        0
                                        • T tomy
                                          11 Jan 2019, 09:19

                                          @mrjj
                                          Hi, good morning. :)

                                          Yes, r2 follows the click presses and there's an update() which makes it happen by re-calling painEvent each time.
                                          My assumption was that, the part of paintEvent below is useful only for the first time when the program runs.

                                          QPainter painter(this);
                                             QRect r1 = rect().adjusted(10, 10, -10, -10);
                                             painter.setPen(QColor("#33B5E5"));
                                             painter.drawRect(r1);
                                             QRect r2(QPoint(0, 0), QSize(40, 40));
                                          

                                          Afterwards when the user clicks different locations on the widget, only the following section is needed and the above one will be excessive.

                                          if(m_lastPos.isNull())
                                                  r2.moveCenter(r1.center()); 
                                              else
                                                  r2.moveCenter(m_lastPos);
                                                painter.fillRect(r2, QColor("#FFBB33"));
                                          }
                                          

                                          Disagree?

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 11 Jan 2019, 10:29 last edited by mrjj 1 Nov 2019, 10:29
                                          #20

                                          @tomy
                                          Hi and good morning
                                          it doesn't really work that way.
                                          You need all of the paint code each time.

                                          T 1 Reply Last reply 11 Jan 2019, 12:18
                                          1

                                          1/23

                                          8 Jan 2019, 17:10

                                          • Login

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