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. Understanding Logic of a given QT function
QtWS25 Last Chance

Understanding Logic of a given QT function

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 5paint eventc++widget
7 Posts 2 Posters 869 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.
  • J Offline
    J Offline
    Jokerking
    wrote on 29 Jun 2020, 20:58 last edited by Jokerking
    #1

    I am new to QT, need help in understanding the below function. Could someone explain the big picture of the below function?
    what does it do in general? Need more incite on what exactly the below paintEvent does.

    A simple comment on the code and explanation of the logic is what I actually need.

    void MyClass::paintEvent(QPaintEvent* event)
    {
        const float m_defaultPaintedTotal = 30.0f;
        const float m_minimumSpacing = 5.0f;
        const size_t m_scrollbarWidth = 8;
    
        const float total = m_total == 0.0f ? m_defaultPaintedTotal : m_total;
        const float widthMinusScrollbars = width() - m_scrollbarWidth; // What is the width units ? pixels or some units ?
        const float ratio = widthMinusScrollbars / total; // can the ratio be zero ?
        const float maxNumberTicks = widthMinusScrollbars / m_minimumSpacing;
    
        const float originalTickSize = total / maxNumberTicks;
    
        static const float fractions[] = { 1 / 60.0f, 1 / 30.0f, 1 / 15.0f, 1 / 10.0f, 1 / 5.0f, 1 / 2.0f, 1, 2, 5, 10, 15, 30, 60, 300 ,600, 1000, 2000, 3600, 5000 };
    
        auto tickSize = std::lower_bound(std::begin(fractions), std::end(fractions), originalTickSize);
        float tickSize = *tickSize * ratio;
        
        QPainter painter(this);                                    //create a QPainter and pass a pointer to the device.A paint device can be a QWidget, a QPixmap or a QImage
        painter.setPen(QPen(Qt::white, 1));                        //create a black pen that has line and the width is 2.
        painter.setBrush(Qt::BrushStyle::SolidPattern);
    
        int h = height();
        int tickCount = 0;
        for (float i = 0; i < widthMinusScrollbars; i += tickSize, tickCount++) {
            const size_t shortTickLength = 10;
            const size_t longTickLength = 15;
            size_t length = tickCount % 5 ? shortTickLength : longTickLength;
            painter.drawLine(i, h, i, h - length);
    
            if (!(tickCount % 10)) {
                float value = i * *tickSize / ratio;
                if (value) {
                    const float boxWidth = 40.0f;
                    const float boxHeight = 20.0f;
                    const int precision = 3;
                    painter.drawText(QRectF(i - boxWidth/2, 0, boxWidth, boxHeight), Qt::AlignHCenter, QString::number(value, 'f', precision));
                }
            }
        }
        int cursorWidth = m_positionCursor.width();;
        int cursorHeight = m_positionCursor.height();
    
        painter.drawPixmap(QRect(m_currentTime * ratio - cursorWidth, h - cursorHeight, cursorWidth, cursorHeight), m_positionCursor);
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Jun 2020, 21:05 last edited by
      #2

      Hi and welcome to the forums

      It seems to draw a ruler or other measure Widget with non linar scale.
      With ticks and text.
      In the end it draw some image. (m_positionCursor)

      Where did you get it from ?

      J 1 Reply Last reply 29 Jun 2020, 21:09
      2
      • M mrjj
        29 Jun 2020, 21:05

        Hi and welcome to the forums

        It seems to draw a ruler or other measure Widget with non linar scale.
        With ticks and text.
        In the end it draw some image. (m_positionCursor)

        Where did you get it from ?

        J Offline
        J Offline
        Jokerking
        wrote on 29 Jun 2020, 21:09 last edited by
        #3

        @mrjj It was a task given to me, I have no idea from where did they gave me. Its a homework and only information what I know is that they are working on 3D application like moviemaker timeline kind of idea.

        M 1 Reply Last reply 29 Jun 2020, 21:11
        0
        • J Jokerking
          29 Jun 2020, 21:09

          @mrjj It was a task given to me, I have no idea from where did they gave me. Its a homework and only information what I know is that they are working on 3D application like moviemaker timeline kind of idea.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Jun 2020, 21:11 last edited by mrjj
          #4

          @Jokerking
          You could put in a widget and see what it paints ?

          Most of its just calculations to place all the ticks and text.

          Note there is abug in it that would prevent it from ever compiling

          auto tickSize = std::lower_bound(std::begin(fractions), std::end(fractions), originalTickSize);
          float tickSize = *tickSize * ratio;

          tickSize is defined twice.

          1 Reply Last reply
          2
          • J Offline
            J Offline
            Jokerking
            wrote on 29 Jun 2020, 21:19 last edited by Jokerking
            #5

            @mrjj I am not a QT user, I even don't have a QT setup ready either. The idea is to understand the concept and mainly the math and logic of the function. And Yes, there might be bugs, so I need to identify that too.

            I am trying it myself and got tired so I decided to post in QT forum for help in decoding the math.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jokerking
              wrote on 29 Jun 2020, 21:44 last edited by
              #6

              @mrjj would you be able to help me in decoding it?

              M 1 Reply Last reply 30 Jun 2020, 19:30
              0
              • J Jokerking
                29 Jun 2020, 21:44

                @mrjj would you be able to help me in decoding it?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 30 Jun 2020, 19:30 last edited by
                #7

                @Jokerking
                Well which part ?

                It loops over the full area , minus the area used for scrollbar

                for r (float i = 0; i < widthMinusScrollbars; i += tickSize, tickCount++) {

                It advances ticksize pr loop

                every 5 tick count

                size_t length = tickCount % 5 ? shortTickLength : longTickLength;

                it shows a long tick.

                then at every 10 tick ( i think) it draws the some text.
                Has a bug there as
                value = i * *tickSize / ratio;
                seems very odd to me.

                Then it draw some image.

                1 Reply Last reply
                3

                7/7

                30 Jun 2020, 19:30

                • Login

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