D
D
Dmitry DD2018-03-18 20:07:52
Qt
Dmitry DD, 2018-03-18 20:07:52

Display file.jpg background on QWidget?

#include<QtWidgets>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
  QWidget wgt;
  wgt.resize(500,500);
  QWidget* w1 = new QWidget(&wgt);
  QPalette pal1;
  pal1.setColor(w1->backgroundRole(),Qt::black);
  w1->setPalette(pal1);
  w1->setGeometry(50,50,100,100);
  w1->setAutoFillBackground(true);

  QWidget* w2 =new QWidget(&wgt);
  QPalette pal2;
  pal2.setBrush(w2->backgroundRole(),QBrush(QPixmap(":/HappyB003.jpg")));
  w2->setPalette(pal2);
  w2->setGeometry(0,200,100,100);
  w2->setAutoFillBackground(true);
  wgt.show();

    return a.exec();
}

The first widget is displayed, but the second is not. Added a photo to resources.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2018-03-18
@IGHOR

Try like this
Well, or put the image in QLabel::setPixmap, then manage the geometry manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question