Adding a label to a widget

前端 未结 3 1998
醉酒成梦
醉酒成梦 2021-01-21 05:49

I am trying to add a label to the main window using Qt. Here is a piece of the code:

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QWi         


        
3条回答
  •  醉梦人生
    2021-01-21 06:04

    You can try:

    QWidget window;
    
    QImage image("yourImage.png");
    QImage newImage = image.scaled(150, 150, Qt::KeepAspectRatio);
    QLabel label("label", &window);
    label.setGeometry(100, 100, 100, 100);
    label.setPixmap(QPixmap::fromImage(newImage));
    
    window.show();
    

    this way you can even decide where to put the label and choose the image size.

提交回复
热议问题