qt选取系统图片后渐入显示到label上
时间: 2024-05-02 11:21:57 浏览: 229
要在Qt中选取系统图片并将其渐入显示到标签上,可以使用QFileDialog和QImage实现。以下是一个简单的示例代码:
```
void MainWindow::on_selectImageBtn_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("Images (*.png *.jpg *.bmp)"));
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("错误"), tr("无法打开该图片!"));
return;
}
ui->imageLabel->setPixmap(QPixmap::fromImage(image));
ui->imageLabel->setScaledContents(true);
ui->imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ui->imageLabel->setStyleSheet("background-color: #CCCCCC;");
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(ui->imageLabel);
ui->imageLabel->setGraphicsEffect(effect);
QPropertyAnimation *animation = new QPropertyAnimation(effect, "opacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
}
```
在这个例子中,当用户单击“选择图片”按钮时,会弹出一个文件对话框,让用户选择要显示的图片。如果用户选择了一个有效的图片,那么就使用QImage加载这个图片,并将其设置为标签的背景。接着,创建一个QGraphicsOpacityEffect对象,将其应用到标签上,并使用QPropertyAnimation将标签的不透明度从0渐变到1,实现了渐入的效果。
阅读全文
相关推荐


















