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. Qt Image on label
Forum Updated to NodeBB v4.3 + New Features

Qt Image on label

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 2 Posters 4.9k Views 1 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome
    Im not sure 100% what you ask, but it sounds like
    that text you write on canvas, has different size sometimes.
    Have you tried to set the size directly ?

    QFont font = painter->font() ;
    font.setPointSize(12);
    /* set the modified font to the painter */
    painter->setFont(font);
    /* draw text etc. */
    painter.drawText(....);
    
    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gayathri
      wrote on last edited by
      #3

      Yes i have tried to set the size inspite of it some of the text comes very large for some background images.

      I have a canvas label. I paint a background scene .. place a character image on the scene and then a dialogue text on the scene. for some of the background images i place on the scene , my dialogue text is very very big . thats my problem . Does this explanation help mrjj ?. Am sorry for being unclear

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

        @Gayathri said:
        HI
        ok. how do you place the dialogue text ?
        Its it a label or do you draw it with drawText?
        Normally, it uses the font you set.

        ANd when you say scene , you mean a QLabel with pixmap you draw on ?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gayathri
          wrote on last edited by
          #5

          Yes it is a QLabel with pixmap and then i use painter and drawText for the dialog . I set the font size

          const QPixmap *orig_bg = canvas_bg_img->pixmap();
          QImage orig_bg_img = orig_bg->toImage();

          QPainter p(&orig_bg_img);
          
          QRect rect(0,350,640,200);
          p.drawRect(rect);
          p.fillRect(rect, QBrush(QColor(0, 0, 0, 128)));
          
          p.setPen(QColor(200,200,180));
          
          
          QRect rect1(50,350, 800, 300);
          QFont font1 = p.font() ;
          font1.setPointSize(12);
          /* set the modified font to the painter */
          p.setFont(font1);
          p.drawText(rect1,Qt::TextWordWrap,narrator);
          
          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            ok. thank you for code.
            So even with this code, it sometimes is way bigger than 12 points?

            I wonder if some of the images makes setPointSize scale.
            Can you try with
            void QFont::setPixelSize(int pixelSize)
            and see if it still makes big text sometimes.
            Then we know why it happens.

            G 1 Reply Last reply
            1
            • G Offline
              G Offline
              Gayathri
              wrote on last edited by
              #7

              Ok i will try that

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

                oh I forgot to ask.
                Can you make it draw big text each time if u use the image that does it?
                I mean, is it repeatable ?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Gayathri
                  wrote on last edited by
                  #9

                  yes every time i use that background pic ..it is big !

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Gayathri
                    wrote on last edited by
                    #10

                    Hi. SetPixel size worked !!!!!! Could you explain to me why is that so ?

                    mrjjM 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      ok. thank you for code.
                      So even with this code, it sometimes is way bigger than 12 points?

                      I wonder if some of the images makes setPointSize scale.
                      Can you try with
                      void QFont::setPixelSize(int pixelSize)
                      and see if it still makes big text sometimes.
                      Then we know why it happens.

                      G Offline
                      G Offline
                      Gayathri
                      wrote on last edited by
                      #11

                      @mrjj

                      Setpixel size worked ! ... Could u explain as to why this worked

                      Also i have another question.

                      Now i have textwrap for my text in drawtext. But it does go out of view. Is it possible to change font to fit the size of the space i have ???? . Changing font dynamically depending on the size of the dialogue and the space

                      1 Reply Last reply
                      0
                      • G Gayathri

                        Hi. SetPixel size worked !!!!!! Could you explain to me why is that so ?

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

                        @Gayathri
                        Super.
                        Well setPointSize is relative to resolution of the device/surface.
                        so when you say 12. it will try to make that look like 12 across many different
                        screens. Its call device independent.

                        setPixelSizeon the other hand just use the value you give it so
                        on other screen, it might look much smaller.

                        so I think setPointSize somehow thinks that your big image is much higher res
                        and then try to help you scale the font.

                        ++ change font to fit the size
                        Well you can code it yes.
                        using
                        QFontMetrics fm(myFont);
                        int width=fm.width(str);
                        you can get size info about what you draw
                        so you can check if it fits inside the rect you have.
                        if NOT, then you can reduce font size and try again
                        until it fits.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          Gayathri
                          wrote on last edited by
                          #13

                          Thanks a lot @mrjj. You were of great help !

                          mrjjM 1 Reply Last reply
                          0
                          • G Gayathri

                            Thanks a lot @mrjj. You were of great help !

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

                            @Gayathri
                            Super. Please mark as solved if possible.

                            Final note:
                            if you make a loop to find font size then make sure
                            to make a counter guard to exit if looping too many times.
                            while ( TestSize(FontSize,str)==false & count++ < 100) {
                            FontSize--;
                            }
                            So you dont hang your app if u ever get a text that just cannot fit :)

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              Gayathri
                              wrote on last edited by
                              #15

                              sure i will ...thanks mrjj

                              1 Reply Last reply
                              0

                              • Login

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