Important: Please read the Qt Code of Conduct -

Understanding Logic of a given QT function

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

  • Lifetime Qt Champion

    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 ?

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

  • Lifetime Qt Champion

    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.

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

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

  • Lifetime Qt Champion

    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.