Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDir as class field
Forum Updated to NodeBB v4.3 + New Features

QDir as class field

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 251 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by DungeonLords
    #1

    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?
    and

    delete dir;
    

    in destructor?

    There are a lot of examples where QDir dir("1.txt"); use in one method only.

    JonBJ 1 Reply Last reply
    0
    • D DungeonLords

      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?
      and

      delete dir;
      

      in destructor?

      There are a lot of examples where QDir dir("1.txt"); use in one method only.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @DungeonLords
      There is no point newing and deleteing a QDir *dir; class member. Just use QDir 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 named 1.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.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved