Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Qpainter, Drawing text in reverse color

    General and Desktop
    3
    6
    1052
    Loading More Posts
    • 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
      Hariprasasth last edited by

      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.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Hariprasasth last edited by

        @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

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

        1 Reply Last reply Reply Quote 3
        • H
          Hariprasasth last edited by

          @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 Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

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

            1 Reply Last reply Reply Quote 0
            • H
              Hariprasasth last edited by

              @mrjj I tried its not working.

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @Hariprasasth last edited by

                @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 Reply Quote 2
                • First post
                  Last post