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.
  • tomyT tomy

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

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #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.

    tomyT 1 Reply Last reply
    1
    • mrjjM mrjj

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

      tomyT Offline
      tomyT Offline
      tomy
      wrote on 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?

      mrjjM 1 Reply Last reply
      0
      • tomyT tomy

        @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?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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.

        tomyT 1 Reply Last reply
        0
        • mrjjM mrjj

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

          tomyT Offline
          tomyT Offline
          tomy
          wrote on 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?

          mrjjM 1 Reply Last reply
          0
          • tomyT tomy

            @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?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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.

            tomyT 1 Reply Last reply
            0
            • mrjjM mrjj

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

              tomyT Offline
              tomyT Offline
              tomy
              wrote on 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?

              mrjjM 1 Reply Last reply
              0
              • tomyT tomy

                @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?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #20

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

                tomyT 1 Reply Last reply
                1
                • mrjjM mrjj

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

                  tomyT Offline
                  tomyT Offline
                  tomy
                  wrote on last edited by
                  #21

                  @mrjj

                  One reason is that QPainter must be in the following code,

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

                  because it doesn't work somewhere else, as you said.
                  What remains is the rectangles, r1, r2. Couldn't we define them somewhere else and only use them in that code?

                  mrjjM 1 Reply Last reply
                  0
                  • tomyT tomy

                    @mrjj

                    One reason is that QPainter must be in the following code,

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

                    because it doesn't work somewhere else, as you said.
                    What remains is the rectangles, r1, r2. Couldn't we define them somewhere else and only use them in that code?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #22

                    @tomy
                    Hi
                    yes Painter only works in paintEvent ( you can also draw on pixmap with it as only other case)

                    You could have r1 and r2 as members of the class but if the window can be resized,
                    then you want to call rect() each time anyway to make sure to use right size.
                    So im not sure there would be any benefit to store the rects.
                    However, color and fonts and images and such things should be stored in class and
                    not loaded/created each time.

                    tomyT 1 Reply Last reply
                    4
                    • mrjjM mrjj

                      @tomy
                      Hi
                      yes Painter only works in paintEvent ( you can also draw on pixmap with it as only other case)

                      You could have r1 and r2 as members of the class but if the window can be resized,
                      then you want to call rect() each time anyway to make sure to use right size.
                      So im not sure there would be any benefit to store the rects.
                      However, color and fonts and images and such things should be stored in class and
                      not loaded/created each time.

                      tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by
                      #23

                      @mrjj
                      OK, thanks so much.

                      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