QLabel边框不会显示pixmap

时间:2021-03-20 22:43:55

I can get a border to display on my QLabels just fine:

我可以在我的QLabel上显示一个边框就好了:

QLabel边框不会显示pixmap

But when I try to display a pixmap in them, the border goes away:

但是当我尝试在其中显示像素图时,边框会消失:

QLabel边框不会显示pixmap

I set the frame properties in the constructor of my QLabel subclass:

我在QLabel子类的构造函数中设置框架属性:

ObjectSlot::ObjectSlot(int index) {
    setIndex(index);

    setFrameShape(QFrame::StyledPanel);
    setFrameShadow(QFrame::Raised);
    setLineWidth(3); 
    setMidLineWidth(3);

    setAlignment(Qt::AlignCenter);
    return;
}

The pixmap is set in the paintEvent:

pixmap在paintEvent中设置:

void ObjectSlot::paintEvent(QPaintEvent* event) {
    QPixmap* image = new QPixmap("://images/Box.png");
    setPixmap(image->scaled(width(),height(),Qt::KeepAspectRatio));
    QLabel::paintEvent(event);
}

Why does the border go away? Why is life so cruel?

为什么边界会消失?为什么生活如此残酷?

1 个解决方案

#1


As doc said:

正如医生所说:

Setting the pixmap clears any previous content. The buddy shortcut, if any, is disabled.

设置像素图会清除以前的任何内容。伙伴快捷方式(如果有)被禁用。

So it seems that it is impossible, but I found next solution, you shouldn't setPixmap(), you need just drawPixmap() when all correct label was painted:

所以它似乎是不可能的,但是我找到了下一个解决方案,你不应该setPixmap(),当绘制所有正确的标签时你只需要drawPixmap():

void ObjectSlot::paintEvent(QPaintEvent *e)
{
    QLabel::paintEvent(e);
    //label painted
    QPainter p(this);
    QPixmap image("G:/2/qt.png");
    p.drawPixmap(QPoint(1,1),image.scaled(100,100,Qt::KeepAspectRatio));
}

Result:

QLabel边框不会显示pixmap

Not the best solution because you should adapt it to your purposes, but currently better than nothing.

不是最好的解决方案,因为你应该根据你的目的调整它,但目前总比没有好。

#1


As doc said:

正如医生所说:

Setting the pixmap clears any previous content. The buddy shortcut, if any, is disabled.

设置像素图会清除以前的任何内容。伙伴快捷方式(如果有)被禁用。

So it seems that it is impossible, but I found next solution, you shouldn't setPixmap(), you need just drawPixmap() when all correct label was painted:

所以它似乎是不可能的,但是我找到了下一个解决方案,你不应该setPixmap(),当绘制所有正确的标签时你只需要drawPixmap():

void ObjectSlot::paintEvent(QPaintEvent *e)
{
    QLabel::paintEvent(e);
    //label painted
    QPainter p(this);
    QPixmap image("G:/2/qt.png");
    p.drawPixmap(QPoint(1,1),image.scaled(100,100,Qt::KeepAspectRatio));
}

Result:

QLabel边框不会显示pixmap

Not the best solution because you should adapt it to your purposes, but currently better than nothing.

不是最好的解决方案,因为你应该根据你的目的调整它,但目前总比没有好。