Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize and declare QFile

Tags:

c++

qt

Can I initizialize a QFile and assign a value in a second time? I tried in this way

...
QFile file;
...
if (i == 0) file = QFile("foo.txt");
else file = QFile("bar.txt");
...

But Qt Creator rises this error: "'QFile& QFile::operator=(const QFile&)' is private within this content"

Can you help me?

like image 648
user3713179 Avatar asked Feb 13 '23 11:02

user3713179


1 Answers

QFile is a QObject, and those are non-copyable and non-assignable. What you're looking for is the setFileName method:

QFile file;
...
file.setFileName(i == 0 ? "foo.txt" : "bar.txt");
...

You could also have

QFile file(i == 0 ? "foo.txt" : "bar.txt");
like image 59
Kuba hasn't forgotten Monica Avatar answered Feb 15 '23 12:02

Kuba hasn't forgotten Monica