[Solved] Update the text using QPainter



  • Dear all,
    (My question is around QPainter. Please move it to right section if its not the relevant place)

    My application is real time, which reads value and updates the same region.

    Here is my code does that

    @
    //Set pen to black.
    m_pPainter->setPen(textColor);

            // Set the font to the QPainter
            m_pPainter->setFont(textFont);
    
            // Fill the background color of the text
            m_pPainter->fillRect(
                relPosX,
                relPosY,
                textWidth,
                textHeight,
                textBgColor
            );
    
            //Draw text at the specified co-ordinates
            m_pPainter->drawText(
                relPosX,
                relPosY,
                textWidth,
                textHeight,
                Qt::AlignLeft | Qt::AlignVCenter,
                displayText
            );
    

    @

    Since this will be executed continuously, it was over writing the text with over lap.

    To avoid that,
    I have added the below line which solved the problem
    @
    m_pPainter->eraseRect(relPosX, relPosY, textWidth, textHeight);
    @

    But, I have another problem because of this.

    What happens is that, eraseRect(...) clears the complete rectangular region, and it erases the color of the background.

    What is the best way to clear the text ("just clearing the text"), without loosing the background?

    Can i get your help please to solve this problem?

    Please let me know if my explanation is not clear.

    Thank you,
    Kumar



  • You should separate out the following.

    1. Identify the static screen. i.e drawing which remains same across different paint event. You draw this in pixmap and keep it ready always.
    2. Over this pix map you should draw dynamic content which is changing every now and then.

    This way will not have the erasing and writing issue.



  • Hi Dheerendra,
    Thanks for your reply.

    But, how it will solve the problem.

    The dynamic content going to be changing frequently.

    Once again, when the dynamic value is changed, its going to overlap on the previous value isnt it?

    Please let me know if my understanding is not correct.

    Thank you,
    Kumar



  • Since your background is fresh slate always and you are writing on that, it should not cause overalp issue. Best thing is will you be able to send the source code ? I can modify and suggest you the approach.



  • You do not show in which context your paint occurs.
    If you use the regular method of painting within the paint event, and invalidating the rectangle that needs to be redrawn whenever necessary, the paint system will take care of all that.
    You only might have to optimize your painting code in case it is too slow or shows flickering (e.g. use caching of mostly static content via Pixmap as suggested above; minimize the size and frequency of the repaints by comparing whether the data has actually changed;...)



  • Hi Dheerendra & Asperamanca,
    Thanks for all your replies.

    Your answers really made sense.

    And I understood the problem.
    The problem was appearing because of the legacy design.

    I could overcome the problem.

    Thanks for your help.

    Sorry for the late reply (I was not in town).

    Regards,
    Kumar


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.