2

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

enter image description here

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

enter image description here

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

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:

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?

Jablonski
  • 18,083
  • 2
  • 46
  • 47
TylerKehne
  • 361
  • 2
  • 12

1 Answers1

2

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:

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:

enter image description here

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

Jablonski
  • 18,083
  • 2
  • 46
  • 47