PDA

View Full Version : Rename a lot of files by QFile and QFileSystemModel



stereoMatching
25th July 2012, 15:42
#include <QApplication>

#include <QDir>
#include <QFileSystemModel>
#include <QString>
#include <QTableView>
#include <QTreeView>

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));

view.show();
//....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);
model.blockSignals(true);
view.blockSignals(true);
for(int i = 0; i != 8000; ++i)
{
QFile file(rootPath + "/kkk" + QString::number(i) + ".jpg");
file.rename("jjj" + QString::number(i) + ".jpg");
}
model.blockSignals(false);
view.blockSignals(false);

//the program will stall
qDebug() << "finish";

return app.exec();
}


The UI would stall (become "blank" one before the the all of the files have changed their name)
I do try multi-thread (boost::thread) and QTimer::singleShot, but it is still stallif
besides, if I using multi-thread if will emit warning like "it is unsafe to use QPixmap in other
thread", so I better don't use it.

I added a progress bar to show the progress, but the progress bar also stall
How could I make the UI become alive when the files being rename?Thanks

wysota
25th July 2012, 18:55
Keeping the GUI Responsive