如何从Qt中的文件加载图像? [英] How to load an image from a file in Qt?

查看:255
本文介绍了如何从Qt中的文件加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我已经搜索了一段时间,却找不到一个认真的答案.问题是我有一个包含某些网址的QString变量,例如"C:/Users/Me/Desktop/image.png".如何打开它并在我的应用程序窗口中显示图像? 我知道这个问题看似微不足道,但是我找不到可行的解决方案.

Generally, I've been searching for a while and could not find a serious answer. The problem is that I've a QString variable containing certain url, e.g. "C:/Users/Me/Desktop/image.png". How to open it and display the image in my application window? I know the problem might seem trivial, however I can't find a working solution.

推荐答案

使用QPixmap加载图像,然后使用QLabel进行显示:

Load the image by using QPixmap and then show it with QLabel:

QString url = R"(C:/Users/Me/Desktop/image.png)";
QPixmap img(url);
QLabel *label = new QLabel(this);
label->setPixmap(img);

ImageViewer示例

这篇关于如何从Qt中的文件加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆