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. drawText with different fonts?
Qt 6.11 is out! See what's new in the release blog

drawText with different fonts?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 1.6k 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.
  • J Offline
    J Offline
    jkwok678
    wrote on last edited by
    #1

    This is my idea, I have a custom Text object with a QString and some other extra data like location and font.
    So the idea is to loop through every bit of text and output to screen. What I try to do is change the font just before the drawText, but it doesn't seem to work. So when I add a text the first time it's ok, but on the 2nd time when it's a different font, both changes to the latest font, and when I use the debugger, the 2 fonts in the 2 texts are different. But this isn't reflected on screen.

    for (std::shared_ptr<Text> currentElement : drawnLayout->getTextList())
        {
            int currentX = currentElement->getLocationX();
            int currentY = currentElement->getLocationY();
            int minCoordinateX = (offsetX * canvasSizeX);
            int maxCoordinateX = ((offsetX+1) * canvasSizeX);
            int minCoordinateY = ((offsetY-1) * canvasSizeY);
            int maxCoordinateY = (offsetY*canvasSizeY);;
            int minDisplayX = (offsetX * canvasSizeX);
            int maxDisplayY = (offsetY*canvasSizeY);
            if (currentX >= minCoordinateX && currentX <= maxCoordinateX)
            {
                if (currentY >= minCoordinateY && currentY <= maxCoordinateY)
                {
                    int displayX = currentX- minDisplayX;
                    int displayY = 0-(currentY - maxDisplayY);
                    setFont(currentElement->getFont());
                    painter.drawText(displayX, displayY, currentElement->getReadableText());
                }
            }
        }
    
    J.HilkJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      Use painter.setFont() instead.

      1 Reply Last reply
      4
      • J jkwok678

        This is my idea, I have a custom Text object with a QString and some other extra data like location and font.
        So the idea is to loop through every bit of text and output to screen. What I try to do is change the font just before the drawText, but it doesn't seem to work. So when I add a text the first time it's ok, but on the 2nd time when it's a different font, both changes to the latest font, and when I use the debugger, the 2 fonts in the 2 texts are different. But this isn't reflected on screen.

        for (std::shared_ptr<Text> currentElement : drawnLayout->getTextList())
            {
                int currentX = currentElement->getLocationX();
                int currentY = currentElement->getLocationY();
                int minCoordinateX = (offsetX * canvasSizeX);
                int maxCoordinateX = ((offsetX+1) * canvasSizeX);
                int minCoordinateY = ((offsetY-1) * canvasSizeY);
                int maxCoordinateY = (offsetY*canvasSizeY);;
                int minDisplayX = (offsetX * canvasSizeX);
                int maxDisplayY = (offsetY*canvasSizeY);
                if (currentX >= minCoordinateX && currentX <= maxCoordinateX)
                {
                    if (currentY >= minCoordinateY && currentY <= maxCoordinateY)
                    {
                        int displayX = currentX- minDisplayX;
                        int displayY = 0-(currentY - maxDisplayY);
                        setFont(currentElement->getFont());
                        painter.drawText(displayX, displayY, currentElement->getReadableText());
                    }
                }
            }
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @jkwok678 by calling setFont on the (presumably) parent widget instead of the QPainter, you set the font on the parent and that is cascading down to its children -> you see only one font for all texts


        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

        • Login

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