3

I am attempting to get a QLabel display an image and some text centered vertically with that image. I can't figure out how to do this. Most sources I've found on the internet suggest using the following code:

ui->label->setText("<img src='c:/images.jpg' style='vertical-align:middle;'> Hello");

However what I get is this. The bottom line of the text is centered near the image, but what I like to have is the middle line of the text to be centered (like so: made in ms paint). Sounds like a pretty common task yet I wasn't able to find any answer.

How can I achieve this?

2
  • Are you looking for a solution with style sheet or not necessarily? If not, the you can just create a QHBoxLayout and put 2 QLabel in it : one with the picture, one with the title
    – IAmInPLS
    Jun 21, 2016 at 8:23
  • Stylesheets is preferred. Yes, I was thinking about the 2-label solution, it just doesn't look right to me
    – O.W.Grant
    Jun 21, 2016 at 8:38

1 Answer 1

0

I tried to run it in my QtCreator and it work fine. So, for start, you can try the following:

  1. Check the picture size. Open it in paint.net or something like that. Perhaps the top image is an empty area of ​​the invisible. Those, the actual size of the image looks like an elongated rectangle, in which the visible part is on the bottom. Try other image.
  2. Try to change the format jpg to png. I had problems with displaying images in jpg format, so I always try to use the png.
  3. Problems with styles. Qt may work incorrect with css style, because it not part of the Qt. I tested in version Qt 5.4.0. If you have an older, may be its time to update it.
2
  • The image is a square, it is located here: imgur.com/hHaLXD4. I tried both jpg and png - same result. I am using Qt 5.6, though. Could you please post a screenshot? Perhaps the font size you use is too small to notice whether the text is centered by the text's bottom line (this is what I have now) or by its center line (this is what I'm trying to achieve)? Try setting a larger font size for the label?
    – O.W.Grant
    Jun 21, 2016 at 9:12
  • @O.W.Grant - Hm... Yes, if I increase the font-size the label seems like this: imgur.com/sxX7tSF. So, but if Iuse this code in jsfiddle: jsfiddle.net/nr6Lme5n it works. I think the reason is that in Qt vertical-align of the label not work. I will try other variants and write if can find a solution.
    – steell911
    Jun 21, 2016 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.