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. [Solved] Why my function cannot call itself?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Why my function cannot call itself?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.1k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    slumberdin
    wrote on last edited by
    #1

    Hello, I'm new to Qt environment.
    Currently, I want to do a recursive in the function.
    What I am doing right now is, I want to copy a folder which have another folder in it and another folder in the folder in the folder and so on and of course, the files too.
    But, when I do the coding as below, the system always crash.

    I already tried to find the cause of this crash, but didn't find.
    Please help me.

    This is what appears in in my Application Output,

    "The program has unexpectedly finished.
    G:\Qt (keje)\Project\build-Hash_Verification2-Desktop_Qt_5_3_MSVC2010_OpenGL_32bit_Microsoft_Visual_C_Compiler_10_0_x86_Local_PC-Debug\debug\Hash_Verification2.exe crashed"

    And this is my code snippet:
    @
    void MainWindow::copy(QString string, QModelIndex indexVoidDest)
    {
    QDir dir(string);
    foreach(const QFileInfo item, dir.entryInfoList())
    {
    if (item.isFile())
    {
    QFile destinationFile,sourceFile;
    QDir destination(indexVoidDest.data(QFileSystemModel::FilePathRole).toString());
    QString fileName(item.fileName());

            destinationFile.setFileName(destination.filePath(fileName));
            sourceFile.setFileName(item.absoluteFilePath()); 
            sourceFile.copy(destinationFile.fileName());
    
        }
    
        else if (item.isDir())
        {
            filemodel->mkdir(indexVoidDest,item.fileName());
            QString StringSe = (string + "/" + item.baseName()); // I think the error is here
             QModelIndex indexSeDest = filemodel->index(indexVoidDest.data(QFileSystemModel::FilePathRole).toString() + "/" + item.baseName());
            copy(StringSe,indexSeDest);  // I think the error is here
        }
    }
    

    }
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      khryleption
      wrote on last edited by
      #2

      Hello,
      You have to use the filter flag "QDir::NoDotAndDotDot" otherwise you will copy the parent and the current directories too. I think your problem comes from here.

      @dir.entryInfoList(QDir::NoDotAndDotDot)@

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Binary91
        wrote on last edited by
        #3

        Hi,

        I found this function to copy dirs/subdirs including its contents recursively on github.com:
        @static bool copyRecursively(const QString &srcFilePath,
        const QString &tgtFilePath)
        {
        QFileInfo srcFileInfo(srcFilePath);
        if (srcFileInfo.isDir())
        {
        QDir targetDir(tgtFilePath);
        targetDir.cdUp();
        if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
        return false;
        QDir sourceDir(srcFilePath);
        QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
        foreach (const QString &fileName, fileNames)
        {
        const QString newSrcFilePath = srcFilePath + QLatin1Char('/') + fileName;
        const QString newTgtFilePat = tgtFilePath + QLatin1Char('/') + fileName;
        if (!copyRecursively(newSrcFilePath, newTgtFilePath))
        return false;
        }
        }
        else
        {
        if (!QFile::copy(srcFilePath, tgtFilePath))
        return false;
        }
        return true;
        } @

        I did not test it, but it looks right :-P

        1 Reply Last reply
        0
        • S Offline
          S Offline
          slumberdin
          wrote on last edited by
          #4

          Thanks to both of you... :)
          Now I can do my recursive... :D

          khryleption, you are right, but need to define more in it...

          Binary91, thanks for giving me idea... It's very helpful... I don't really understand the code and I don't really know to call the function... :P

          So, the solution, I combine both of your idea and create a new one... :D

          @
          foreach(const QFileInfo item, dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System));
          @

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Binary91
            wrote on last edited by
            #5

            You're welcome.

            Great to hear that it works for you, but if you're interested in something from the algorithm above that you don't understand, just ask for it ;-)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              slumberdin
              wrote on last edited by
              #6

              Thanks, Binary91...

              Maybe you can try to explain on the following question:

              1. How do we call the function in the main cpp? For example,

              @
              copyRecursively(sourceFile, targetFile)
              @

              Is it true??? Just the bool function need to return the value whether it is true or false, does it???

              2)Why there must be static??? Maybe you can explain a little about it...

              Thanks in advance if you can answer it...

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Binary91
                wrote on last edited by
                #7

                1: Yes, exactly. You just pass the sourceFile (folder you want to copy) and targetFile (path where the source should be copied to).
                The boolean return value informs you whether copying was successful or not.

                2: It doesn't "have to" be static, but a static function can be called without creating an object first, hence you can use this function everywhere in your code to copy folders without creating instances of a class.
                This functionallity is useful for often used functions. An example is the public static function QFile::exist(). You can check for existance of any file anywhere in your code and you don't have to create a QFile object first to call it ;-)

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  You can use QDirIterator: it will not be recursive, there will be less code, and it will work faster (this class uses some important IO optimizations).

                  Like so:
                  @
                  QDirIterator it("directory/path", QDir::NoDotAndDotDot | QDir::Files,
                  QDirIterator::Subdirectories);
                  while (it.hasNext()) {
                  it.next();

                  // mkpath to the new file, using it.fileInfo()
                  // copy, using it.fileInfo().
                  

                  }
                  }
                  @

                  (Z(:^

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    slumberdin
                    wrote on last edited by
                    #9

                    Binary91,
                    thanks for the clarification...

                    sierdzio,
                    I will try to use it... :)

                    1 Reply Last reply
                    0

                    • Login

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