QDir as class field
-
I want to use dir
QDir dir("1.txt");
in different methods of my object.
Is it good way to make *QDir dir; field in class like:
class MainWindow : public QMainWindow { Q_OBJECT private: QDir *dir; }
?
And does I need to use
dir = new QDir("1.txt");
in conscructor?
anddelete dir;
in destructor?
There are a lot of examples where QDir dir("1.txt"); use in one method only.
-
@DungeonLords
There is no pointnew
ing anddelete
ing aQDir *dir;
class member. Just useQDir dir;
for it.You can set the path in the declaration, or use void QDir::setPath(const QString &path) when you want to do so.
Note that using
QDir
anywhere with a relative path as you do ("1.txt"
) is "likely to be wrong". That would look for it relative to the application's current working directory, and I doubt you know or intend that. Use absolute paths to find files. Use QStandardPaths Class for certain common directory paths. Not to mention: do you really have a directory named1.txt
??There are a lot of examples where QDir dir("1.txt"); use in one method only.
Depends whether you want to use the same
QDir
instance in multiple places in the class, and care that it should not be reconstrcuted.