[Qt-interest] QPrcoess about stack and heap

pengliang(彭亮) pengliang at founder.com
Mon Mar 28 05:31:38 CEST 2011


Hi All

         I changed “QProcess *tarProcess = new QProcess()” to “QProcess *tarProcess”, please see the picture below,  why? It is perfect when “QProcess *tarProcess = new QProcess()”, 

 

 

----------------------------          picture          -----------------------------------

 

 

 

----------------------------         code     ---------------------------------

 

    // begin of backup

    QString tgzSize;

   //QProcess *tarProcess = new QProcess();

QProcess *tarProcess;

 

   // QObject::connect(tarProcess,SIGNAL(error(QProcess::ProcessError)),,SLOT(readProcessError(QProcess::ProcessError)));

    QString tarCommandPath = "tar";QString tarTgzPathName;

    // dirs backup

    for(int i=0;i<dirsSourceDirList.count();++i)

    {

        int dirLengthInt = QString(dirsSourceDirList[i]).length();

        if ( cutMode == 1 )

        {

            if ( dirLengthInt < beginCut )

            {

                tempDirNameStr.clear();;

            } else {

                tempDirNameStr = QString(dirsSourceDirList[i]).mid(beginCut,1);

            }

        } else if ( cutMode == 2 )

        {

            if ( dirLengthInt < beginCut )

            {

                tempDirNameStr.clear();;

            } else {

                if ( dirLengthInt < endCut )

                {

                    tempDirNameStr = QString(dirsSourceDirList[i]).mid(beginCut,dirLengthInt-beginCut);

                } else{

                    tempDirNameStr = QString(dirsSourceDirList[i]).mid(beginCut,endCut-beginCut);

                }

            }

        }

 

         tarTgzPathName = dirsDestDirList[i] + QDir::separator() + tempDirNameStr;

 

        QDir sourceDir(dirsSourceDirList[i]);

        QDir destDir(tarTgzPathName);

        if (!sourceDir.exists())

        {

            errLogStrList << "Dirs backup: " + sourceDir.path() + " does not exist!" ;

            continue;

        }

        if (! destDir.exists() )

        {

            if ( ! QDir::root().mkpath(destDir.path()) )

            {

                errLogStrList << "Dirs backup: " + destDir.path() + " can not create!";

                continue;

            }

        }

 

        currentDir.setCurrent(dirsSourceDirList[i]);

        tarTgzPathName.append(QDir::separator() + QString(dirsSourceDirList[i]).split("/").last() + ".tgz");

        tarProcess->start(tarCommandPath,QStringList() << "zcf" << tarTgzPathName << dirsSourceDirList[i]);

        if ( tarProcess->waitForFinished(-1))

        {

            if ( tarProcess->exitStatus() != QProcess::NormalExit )

            {

                errLogStrList << "Dirs backup: " + destDir.path() + "  tar process exit with error!";

                continue;

            }

        }

        tempFileInfo.setFile(tarTgzPathName);

        double tempFileSizeDouble = tempFileInfo.size()/1024.0/1024.0;

        tgzSize.setNum(tempFileSizeDouble,'g',4);

        okLogStrList << "Dirs backup: " + sourceDir.path() + "  to  "  + destDir.path() + "  ok. (size: " + tgzSize + " M)" ;

    }

 

    // remove duplicate jobs

    tempStrList = needBackupJobDestList;

    tempStrList.removeDuplicates();

    foreach(tempStr,tempStrList)

    {

        tempDir.setPath(tempStr);

        if ( ! tempDir.exists() )

        {

            QDir::root().mkpath(tempStr);

            // if false how to do

        }

    }

 

  // the dest directory name of jobs backup

    beginCut = 0;endCut = 0;cutMode = 0;

    tempDirNameStr.clear();;

    if ( jobsBackNameMode == "STRING" )

    {

        tempStrList = jobsBackNameString.split("-");

        if ( tempStrList.count() == 2 )

        {

            cutMode = 2;

            beginCut = QString(tempStrList[0]).toInt(&tempBool,10);

            endCut = QString(tempStrList[1]).toInt(&tempBool,10);

        } else if ( tempStrList.count() == 1 ){

            cutMode = 1;

            beginCut = QString(tempStrList[0]).toInt(&tempBool,10);

        } else {

            cutMode = 0;

            tempDirNameStr.clear();

        }

    } else if ( jobsBackNameString == "CUSTOM" )

    {

        if ( jobsBackNameString.isEmpty() )

        {

           cutMode = 0;

           tempDirNameStr.clear();

       } else {

           cutMode = 3;

           tempDirNameStr = jobsBackNameString;

       }

    } else {

        if ( jobsBackNameString == "YEAR")

        {

            cutMode = 4;

            tempDirNameStr = currentDateTime.toString("yyyy");

        } else if ( jobsBackNameString == "MONTH" )

        {

            cutMode = 5;

            tempDirNameStr = currentDateTime.toString("MM");

        } else if ( jobsBackNameString == "MONTH" )

        {

            cutMode = 6;

            tempDirNameStr = currentDateTime.toString("dd");

        } else {

            cutMode = 7;

            tempDirNameStr = currentDateTime.toString("yyyyMMdd");

        }

    }

 

    // jobs backup

    int jobLengthInt;

    for(int i=0;i<needBackupJobList.count();++i)

    {

        tempStr = needBackupJobDestList[i] + QDir::separator();

        jobLengthInt = QString(needBackupJobNameList[i]).length();

        if ( cutMode == 1 )

        {

            if ( jobLengthInt < beginCut )

            {

                tempDirNameStr.clear();;

            } else {

                tempDirNameStr = QString(needBackupJobNameList[i]).mid(beginCut,1);

            }

        } else if ( cutMode == 2 )

        {

            if ( jobLengthInt < beginCut )

            {

                tempDirNameStr.clear();;

            } else {

                if ( jobLengthInt < endCut )

                {

                    tempDirNameStr = QString(needBackupJobNameList[i]).mid(beginCut,jobLengthInt-beginCut);

                } else{

                    tempDirNameStr = QString(needBackupJobNameList[i]).mid(beginCut,endCut-beginCut);

                }

            }

        }

 

        tempStr = needBackupJobDestList[i] + QDir::separator() + tempDirNameStr;

        tempDir.setPath(tempStr);

        if (!  tempDir.exists() )

        {

            if ( ! QDir::root().mkpath(tempStr) )

            {

                continue;

            }

        }

 

        tempStr = tempStr + QDir::separator() + needBackupJobNameList[i] + ".tgz";

        if ( currentDir.setCurrent(QString(needBackupJobList[i])) )

        {

            tarProcess->start(tarCommandPath,QStringList() << "zcf" << tempStr << needBackupJobNameList[i]);

        } else {

            tarProcess->start(tarCommandPath,QStringList() << "zcf" << tempStr << needBackupJobList[i] + needBackupJobNameList[i]);

        }

 

        qDebug() << "here";

 

        if ( tarProcess->waitForFinished(-1))

        {

            if ( tarProcess->exitStatus() != QProcess::NormalExit )

            {

                // write error log

                errLogStrList << "Jobs backup: " + needBackupJobList[i]  + needBackupJobNameList[i] + " : error!";

                continue;

            }

            //write ok log

            tempFileInfo.setFile(tempStr);

            double tempFileSizeDouble = tempFileInfo.size()/1024.0/1024.0;

            tgzSize.setNum(tempFileSizeDouble,'g',4);

            okLogStrList << "Jobs backup: " + needBackupJobList[i] + needBackupJobNameList[i] + "  to  "  + tempDir.path()  + "  ok. (size: " + tgzSize + " M)" ;;

        }

 

    }

 

    // current time

    currentDateTime = QDateTime::currentDateTime();

    currentDateTimeStr = currentDateTime.toString("yyyy/MM/dd hh::mm::ss");

    okLogStrList << "--------------  End:  " + currentDateTimeStr + "   -----------------------";

 

    // delete tarProcess

    //delete tarProcess;

 

    // write qstringlist to log file. if false, write to current log file.

    if ( okLogStrList.count() != 1 )

    {

        tempBool = writeLogFile(okLogFilePath,okLogStrList);

        if ( ! tempBool )

        {

             writeLogFile(currentOkLogFilePath,okLogStrList);

        }

    }

 

    if ( errLogStrList.count() != 1 )

    {

        tempBool = writeLogFile(errLogFilePath,errLogStrList);

        if ( ! tempBool )

        {

             writeLogFile(currentErrLogFilePath,errLogStrList);

        }

    }

 

 

 

Thanks

Ken

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110328/db60d6cd/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 37940 bytes
Desc: image001.png
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110328/db60d6cd/attachment.png 


More information about the Qt-interest-old mailing list