QFileSystemModel crash when rename



  • [code]
    #include <QtCore>
    #include <QtGui>

    int main(int argc, char **argv)
    {
    QApplication app( argc, argv );

    QFileSystemModel model;    
    QString const rootPath = "E:/file_test/very big";
    
    QTableView view;
    view.setModel(&model);
    view.setRootIndex(model.setRootPath(rootPath));    
    
    QSplitter splitter;
    splitter.addWidget(&view);   
    splitter.show();    
    
    return app.exec();
    

    }
    [/code]

    Under the folder "very big" consist 8448 jpg, when I using a tool(Bulk rename utility)
    to rename the jpg under "very big", the program will crash.
    How could I stop the self updating function of QFileSystemModel?
    Thanks



  • 1 : I find out the QDirModel will not update until you call "refresh"

    2 : even I do not setup the model for the view, rename the files will affect
    the main thread(GUI) seriously.No wonder why even I rename the files
    on another thread, the GUI still stall.

    [code]
    int main( int argc, char **argv )
    {
    QApplication app( argc, argv );

    QFileSystemModel model;
    
    QString const rootPath = "E:/file_test/very big";
    model.setRootPath(rootPath);       
    
    QTableView view;
    //I haven't set any model for The view, but the program still
    //crash if I rename 8448 of my jpg files by other program    
    
    QSplitter splitter;
    splitter.addWidget(&view);   
    splitter.show();
    
    return app.exec&#40;&#41;;
    

    }
    [/code]

    There are still more than 1XX pages of the model/view architecture I have to studied(advanced Qt programming)
    I hope I could know how to prevent the automatic "refresh" function of the QFileSystemModel after
    I finish the chapter 4, 5, 6 in the book

    QDirModel is not a good solution, but looks like it could solve my problem for now.

    Never expect that customize the model/view of Qt4 could be so complicated(2XX pages to study)
    Maybe a little bit difficult for a newbie like me?



  • The book is called Advanced Qt Programming. How did you figure that that is suitable for a beginner with Qt?



  • bq. The book is called Advanced Qt Programming. How did you figure that that is suitable for a beginner with Qt?

    So it is not suitable for a beginner?But this book explain model/view much more detail than
    "Foundation of Qt development", although "Foundation of Qt development" teach us how to
    deal with table model/view but no tree model/view.

    Whatever, when I use multithread to rename the files with QFileSystemModel,
    the GUI still stall and the use of memory will keep raising(memory leak?),but QDirModel
    would not have the situations like QFileSystemModel.

    Is this a bug of QFileSystemModel?Or a normal behavior?

    worker thread
    [code]
    #include <QtCore>
    #include <QtGui>

    class FileRenameRunnable: public QThread
    {

    Q_OBJECT

    public :

    FileRenameRunnable(QString const rootPath)
    : rootPath_(rootPath)
    {
    }
    
    virtual void run()
    {
        for(int i = 0; i != 8448; ++i)
        {
          QFile file&#40;rootPath_ + "/kkk" + QString::number(i&#41; + ".jpg"&#41;;
          file.rename(rootPath_ + "/jjj" + QString::number(i&#41; + ".jpg");
          emit valueChanged(i);
        }
    }
    

    signals:
    void valueChanged(int value);

    private:
    QString rootPath_;
    };
    [/code]

    main thread
    [code]
    #include <QtCore>
    #include <QtGui>

    #include "fineNameRunnable.hpp"

    int main(int argc, char **argv)
    {
    QApplication app( argc, argv );

    QFileSystemModel model;
    QString const rootPath = "E:/file_test/very big";
    
    QTableView view;
    view.setModel(&model);
    view.setRootIndex(model.setRootPath(rootPath));
    
    QProgressBar progress;
    progress.setRange(0, 8447);
    
    //....select the data you want to rename from the view
    //let us assume I select 8000 of jpg files
    
    //don't know how to rename the file by QFileSystemModel, so I use QFile instead
    QDir::setCurrent(rootPath);
    FileRenameRunnable *myTask = new FileRenameRunnable(rootPath);
    app.connect(myTask, SIGNAL(finished()), myTask, SLOT(deleteLater()));
    app.connect(myTask, SIGNAL(valueChanged(int)), &progress, SLOT(setValue(int)));
    
    QSplitter splitter;
    splitter.addWidget(&view);
    splitter.addWidget(&progress);
    splitter.show();
    

    //the program will stall even you run under another thread
    //but QDirModel will not if you run under another thread
    myTask->start();

    qDebug() << "finish";
    
    return app.exec&#40;&#41;;
    

    }
    [/code]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.