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. Qpainter, Drawing text in reverse color

Qpainter, Drawing text in reverse color

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.5k 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.
  • H Offline
    H Offline
    Hariprasasth
    wrote on last edited by
    #1

    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

    J.HilkJ 1 Reply Last reply
    0
    • H Hariprasasth

      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

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • H Offline
        H Offline
        Hariprasasth
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

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

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hariprasasth
            wrote on last edited by
            #5

            @mrjj I tried its not working.

            mrjjM 1 Reply Last reply
            0
            • H Hariprasasth

              @mrjj I tried its not working.

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

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

              1 Reply Last reply
              2

              • Login

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