简体   繁体   中英

How to set indention to QImage in QLabel?

I have a QImage and wants to set it on a Qlabel . For that I am using QPixmap . Something like this

QPixmap pixmap(QPixmap::fromImage(my_qimage));
mLabel->setIndent(42);
mLabel->setPixmap(pixmap);

Here I want to set the image after an indention of 42 pixels. But it is not working with Pixmap. Although I tried the same with text like this

mLabel->setIndent(42);
mLabel->setText("image");

and it is working fine.

So my question is how can I set the image after an indentation of some pixels on a QLabel ? Any help will be appreciated. If there is an alternate way to achieve such behavior please suggest.

You probably are looking for setMargin(int) property : Doing:

mLabel->setMargin(42);

Should solve your problem.

However, if you surpass the size of half QLabel width (160 pixels in this example of 320 width QLabel ), the QPixmap image won't show. In this case it is necessary to change the alignment property to Right doing next:

mLabel->setAlignment(Qt::AlignLeading|Qt::AlignRight|Qt::AlignVCenter);

In case that you need to set a margin that is above of the half of the size of QLabel, you'll need to calculate the margin doing next:

width_of_label - desiredmargin + width_of_image;

Which in this example of a 320 width QLabel and a 20 pixels width image) is next (using 220 as desired margin ):

320 - 220 - 20 = 80;

So after setting the previous allignment, you should use:

mLabel->setMargin(80);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM