Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qpainter, Drawing text in reverse color



  • Hello,

    I am using QBitmap and QPainter to draw a text with underline option.

    I am implementing two cases: 1. Draw text 2. Draw text in reversed/inverted color - both text and background

    Problem here is, when I reverse the background and pen color the underline is not drawn.

       QBitmap m_hBitmap(476,568);
       QPen m_hPen(Qt::SolidLine);
       QBrush	m_hBrush(Qt::SolidPattern);
       QPainter m_hdcMem;
       QImage image;
    
       //Initialize QPainter with Bitmap
       m_hBitmap.clear();
       m_hdcMem.begin(&m_hBitmap);
       m_hdcMem.setBackgroundMode(Qt::OpaqueMode);
    
       //Initialize Brush & Pen
       m_hBrush.setColor(Qt::color1);
       m_hPen.setWidth(2);//1
       m_hPen.setColor(Qt::color1);
       m_hPen.setCapStyle(Qt::FlatCap);
       m_hPen.setJoinStyle(Qt::MiterJoin);
    
       //Set to QPainter
       m_hdcMem.setPen(m_hPen);
       m_hdcMem.setBrush(m_hBrush);
    
       //Draw text
       //Normal
       QRect rect(50,50,125,100);
       QRect rect_rev(50,150,125,100);
    
       QFont font;
       font.setFamily(QString("Liberation Sans"));
       font.setBold(false);
       font.setUnderline(true);
       m_hdcMem.setFont(font);
       m_hdcMem.drawText(rect,QString("My test text"));
    
    
       //Reverse the backgrond & text color
       if(m_hBrush.color()!=Qt::color1)
           m_hBrush.setColor(Qt::color0);
       else
           m_hBrush.setColor(Qt::color1);
       m_hdcMem.setBrush(m_hBrush);
       m_hdcMem.setBackground(m_hBrush);
    
       if(m_hPen.color()!=Qt::color1)
           m_hPen.setColor(Qt::color1);
       else
           m_hPen.setColor(Qt::color0);
       m_hdcMem.setPen(m_hPen);
       m_hdcMem.drawText(rect_rev,QString("My test text"));
    
       //Save as image
       image = m_hBitmap.toImage();
       image.save("test1.bmp",0,-1); //Testing
    
       //end
       if(m_hBitmap.isNull()!= true && m_hdcMem.isActive()){
           m_hdcMem.end();
       }
       m_hBitmap.clear();
    

    ![alt text](![0_1580740633302_test1.bmp](Uploading 100%) image url)test1.png

    Could someone help me on this?

    Best regards,
    Hari


  • Moderators

    @Hariprasasth
    no, I think the underline is there, in white, right at the edge of the image, that sits on a white background.

    That's why you don't see it :P



  • @J-Hilk You are right. I am implementing like below picture. Could you give some ideas to do that?

    ![alt text](IMG_20200203_202517.jpg image url)


  • Lifetime Qt Champion

    Hi
    Could you not just make the bg rect slightly bigger ?
    QRect rect_rev(50,150,125,100+12);



  • @mrjj I tried its not working.


  • Lifetime Qt Champion

    @Hariprasasth
    Hi
    Its due to setBackground.

    You can just cheat and fill the rect your self
    .....
    m_hdcMem.fillRect(rect_rev, m_hBrush); // fill it
    m_hdcMem.drawText(rect_rev,QString("My test text"));

    alt text

    You can use FontMetric class to get the rect needed for the text-only.


Log in to reply