0

I was trying to create/save a file in qt creator with QFile.open(). But it is always creating/saving files in build folder of application. I have tried this code to change the dir to my directory which inside the directory where I have my source/header files.

QDir::setCurrent("/ui");
file.setFileName("tmp.ui");

QDir::setCurrent("/main/ui");

if (file.open(QIODevice::ReadWrite))
{
    builder.save(&file, myDialog);
}

It creates the file tmp, but not in right directory. I would like to put relative directory. Any idea how I can reach it?

amol01
  • 1,823
  • 4
  • 21
  • 35

1 Answers1

0

I don't think you really need to change current directory. You can generate absolute file path to file without changing current directory:

QDir dir("/main/ui");
file.setFileName(dir.absoluteFilePath("tmp.ui"));

However it seems to be pointless too. You can just write file.setFilename("/main/ui/tmp.ui").

Paths starting with '/' are absolute. Are you sure that you have '/ui' or '/main' directories in your filesystem root? It's not likely. Try to specify valid absolute path to QDir. If you want to specify relative path, it will be searched from the initial current directory (which can, for example, be the build directory).

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127