相关文章推荐
活泼的番茄  ·  SKStoreReviewControlle ...·  2 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I wish to use QImage in Qt to load and save images. While I can load an image, for whatever reason it wont let me save the image.

So I started writing a simple code, made a simple png test file using paint, put it into the same folder as the project itself.

#include <QImage>
#include <iostream>
int main(){
    QImage image;
    image.load("test.png");
    if (image.isNull()){
        std::cout << "ERROR!\n";
    else{
        std::cout << "IMAGE LOADED!\n";
    image.save("test1.png");
    return 0;

During running the program I get the message of "IMAGE LOADED!" from the application output, however when I check the folder I expect the same image saved as test1.png, which doesn't appear at all.

So, how do I actually save an image? What did I miss?

This code works as is. I could open and save the image. You may want to double check that you're looking in the correct folder (shadow builds etc. can be confusing). I used Qt Kit version 5.15.2. – L.C. Nov 8, 2022 at 16:18 "Returns true if the image was successfully saved; otherwise returns false." - what is the return value of your image.save("test1.png"); ? Maybe read-only working directory or sth similar. – pptaszni Nov 8, 2022 at 16:18 @L.C. you mean the newly generated file should be in the same folder as the project? I assumed it would be there? Where exactly do I check? – user19905318 Nov 9, 2022 at 14:28 @pptaszni You mean the return value of image.save() function? If I use following code ` int i = image.save(string); std::cout << i << "\n";` it returns me a 1. – user19905318 Nov 9, 2022 at 14:33 @user19905318 When you run your application from the IDE (supposedly Qt Creator?) the working directory is set in the "Run Settings" in the "Projects" tab on the left. Your app is started from that path, no matter where the executable is placed. Thus, the image may not be written where you expect, but in the working directory set there. In my test, the image was written in the shadow build folder. – L.C. Nov 9, 2022 at 16:07

In the other comments mentioned the image got saved into the working directory, which is not the same as the folder where the project is located.

In my specific case I got the image by adding a full path to the project like

int i = image.save("C:\\Users\\UserName\\Documents\\QT\\testing_ground_cpp\\" + string);

to find the image.

Ideally I should directly access with the working directory.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.