关于QLabel无法显示的问题

问题是这样的:
首先创建一个QLabel,然后加载图片到QLabel上,并将父设置为一个QWidget,结果运行发现整个QLabel无法显示!
贴上出现这个问题的代码:

QLabel *label=new LevelLabel(this);
label->setGeometry(50505050);
label->setPixmap(QPixmap("D:/background.png").scaled(
                        label->size(),
                        Qt::IgnoreAspectRatio,
                        Qt::SmoothTransformation)); // 使用平滑的缩放方式
label->setAttribute(Qt::WA_TranslucentBackground,true);
label->show();

解决方案:
需要对该label设置AutoFillBackground属性

labelLeft->setAutoFillBackground(true);

修改后代码如下:

QLabel *label=new LevelLabel(this);
label->setGeometry(50505050);
label->setPixmap(QPixmap("D:/background.png").scaled(
                        label->size(),
                        Qt::IgnoreAspectRatio,
                        Qt::SmoothTransformation)); // 使用平滑的缩放方式
label->setAttribute(Qt::WA_TranslucentBackground,true);
label->setAutoFillBackground(true);
label->show();

你可能感兴趣的:(qt学习)