QPixmap不能使用存储在变量中的图像路径
QPixmap not working with image path stored in variable
当我像这样直接写入图像路径
QPixmap imgBtry("/some/path/resources/Battery100.png");
它工作得很好,但当我将路径存储在变量中时,它就不起作用了。我该怎么办?以下是完整的代码:
//global variables
std::string path;
std::string img100 = "/some/path/resources/Battery100.png";
std::string img75 = "/some/path/resources/Battery75.png";
std::string img50 = "/some/path/resources/Battery50.png";
std::string img25 = "/some/path/resources/Battery25.png";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap imgBtry(img50);
ui->label_2->setPixmap(imgBtry.scaled(50,50,Qt::IgnoreAspectRatio,Qt::FastTransformation));
}
你得到什么错误?可能是:
QPixmap构造函数接受QString作为实参。当您直接放入字符串时,它可以工作,因为它是一个c-string (char *),并且QString有一个构造函数将c-string作为输入。但是没有以std::string作为输入的构造函数。
:
1)将字符串定义为c-string。
或
2)在调用QPixmap:
之前将std::string转换为c-stringQPixmap imgBtry(img50.c_str());
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何将更多文件夹添加到c++include路径
- 以跨平台的方式将图像源路径从C++发送到 QML
- 使用qdatetime-无法将图像写入路径
- 获取位于安卓工作室中可绘制的图像的绝对路径,用于NDK项目
- 从STD :: String获取QPIXMAP动态图像路径
- Qt表查看单元格中的显示图像并在单击时获取路径
- SDL2:如何沿正方形路径移动图像
- 使用创建跟踪栏设置图像路径
- 从文件中读取的空图像(使用文件路径)
- 使用图像的路径显示图像
- Qt和Android-从图库中的图像获取路径
- 如何使用XCode IDE在SFML中通过相对路径加载图像
- 从SFML中的相对路径加载图像
- QPixmap不能使用存储在变量中的图像路径
- 如何获取txt文件中图像路径的根文件夹名称
- Visual c++图像文件路径转换成xml