QFileSystemModel經常導致主線程反應遲緩

stereoMatching 2012-08-02 10:57:49

副線程

#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(rootPath_ + "/kkk" + QString::number(i) + ".jpg");
file.rename(rootPath_ + "/jjj" + QString::number(i) + ".jpg");
emit valueChanged(i);
}
}

signals:
void valueChanged(int value);

private:
QString rootPath_;
};


主線程

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

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

myTask->start();

qDebug() << "finish";

return app.exec();
}


不但主線程會被“擋住”,而且連記憶體的使用量也是直線飆升

如果使用的是QDirModel就不會有這種情形
開另外一個線程重新命名,不但主線程運作流暢無比
而且記憶體的使用量和單線程的時候比較也是相差無幾

請問這是QFileSystemModel的bug,或者是我這個新手使用不當?
如果這是bug,請問我該到哪裡通報(或者那個好心人幫我報告也好)?
謝謝
...全文
121 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
stereoMatching 2012-08-05
  • 打赏
  • 举报
回复
无论是QDirModel或者是QFileSystemModel我的写法都一样
只差在QDirModel不用设定rootPath

目前我是靠Document中“极度不推荐”的QDirModel解决当下的问题
希望能够有一个办法解决QFileSystemModel阻塞的问题
谢谢
stereoMatching 2012-08-05
  • 打赏
  • 举报
回复
>i会一直疯狂的增长下去,一直到很大的数;


#include <iostream>

int main()
{
int sum1 = 0;
for(int i = 0; i != 8448; ++i)
sum1 += i;
std::cout << sum1 << std::endl;

int sum2 = 0;
for(int i = 0; i < 8448; ++i)
sum2 += i;
std::cout << sum2 << std::endl;

std::cout<<std::boolalpha << (sum1 == sum2)<<std::endl;

return 0;
}


>对于一般的数值类型,如int,double都是可以的
这只是习惯的问题而已,偏好generic的人比较喜欢使用!=
feilinhe 2012-08-03
  • 打赏
  • 举报
回复
对了对于STL使用的迭代器当然不能用“<”,而对于一般的数值类型,如int,double都是可以的,兄弟你刚开始学习编程的吧
feilinhe 2012-08-03
  • 打赏
  • 举报
回复
对于STL来说it != std::end(data)表示不是最后的元素(空指针)
而你的for(int i = 0; i != 8448; ++i)的含义与此不一样,你的这个表示仅仅i不等于8448就可以了,i会一直疯狂的增长下去,一直到很大的数;
for(int i = 0; i < 8448; ++i):表示i只能是小于8448也就是最大i = 8447,明白吗??
stereoMatching 2012-08-03
  • 打赏
  • 举报
回复
是否应该是:for(int i = 0; i < 8448; ++i)

这两者是一样的,只是写程式的习惯不同而已
这种写法在generic programming中很常见
例如

std::list<int> data;
.......
for(auto it = std::begin(data); it != std::end(data); ++it)
{
........
}

如果你用 < 的话就无法通过编译了,但是不管是std::vector,std::list, std::map等
容器的iterator都支援!=,所以喜欢generic programming的人通常会选用!=胜于 <
feilinhe 2012-08-03
  • 打赏
  • 举报
回复
class FileRenameRunnable: public QThread中 for(int i = 0; i != 8448; ++i)
是否应该是:for(int i = 0; i < 8448; ++i)

21,437

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧