简体   繁体   English

如何在QLabel中设置缩进到QImage?

[英]How to set indention to QImage in QLabel?

I have a QImage and wants to set it on a Qlabel . 我有一个QImage并希望在Qlabel上设置它。 For that I am using QPixmap . 为此,我使用的是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. 在这里,我想在42像素的缩进后设置图像。 But it is not working with Pixmap. 但它不适用于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 ? 所以我的问题是如何在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: 您可能正在寻找setMargin(int) 属性 :执行:

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. 但是,如果超过半QLabel宽度的大小(在此宽度为320宽度QLabel示例中为160像素),则不会显示QPixmap图像。 In this case it is necessary to change the alignment property to Right doing next: 在这种情况下,有必要将alignment属性更改为Right,进行下一步:

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: 在你需要设置页边距上述QLabel大小的一半的情况下,你需要计算保证金做下一个:

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宽度QLabel和20像素宽度图像的示例中(使用220作为期望的边距 ):

320 - 220 - 20 = 80;

So after setting the previous allignment, you should use: 因此在设置上一个对齐后,您应该使用:

mLabel->setMargin(80);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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