0

Here is my Model, that inherits QFileSystemModel

class MyFileSysModel : public QFileSystemModel
{
    Q_OBJECT
public:
    MyFileSysModel( QObject *parent = 0);
    Qt::ItemFlags  flags(const QModelIndex &index) const;
    bool dropMimeData(const QMimeData *data,
    Qt::DropActions supportedDropActions() const;
};

in MainWindow I created model and initializied treeview

 MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        model = new MyFileSysModel(this);
        model->setRootPath("/");
       ui->treeView->setModel(model);

         ui->treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
         ui->treeView->setDragEnabled(true);
         ui->treeView->viewport()->setAcceptDrops(true);
         ui->treeView->setDropIndicatorShown(true);
         ui->treeView->setDragDropMode(QAbstractItemView::DragDrop);
         ui->treeView->setAcceptDrops(true);
         ui->treeView->setDefaultDropAction(Qt::MoveAction);
    }

When user drag and drop file it is copying to other directory in separate thread

 class MoveFilesTask : public QObject, QRunnable
    {
        Q_OBJECT
        void run()
        {
            QFile source("source_file_name");
            source.open(QIODevice::ReadOnly);
            QFile destination("some_destination");
            destination.open(QIODevice::WriteOnly);
            QByteArray buffer;
            int chunksize = 200;

            while(!(buffer = source.read(chunksize)).isEmpty())
            {
                destination.write(buffer);
            }
            destination.close();
            source.close();
        }
       void MoveFilesTask::runFilesTransfer(QString source, QString destination)
        {
           QThreadPool::globalInstance()->start(this);
        }
};

File is copying but the GUI in MainWindow with my treeview doesn't work well, it is freezing and blocking sometimes. I think this is because my model updating very often. How can I solve this and prevent updating very often ?

Farhad
  • 4,119
  • 8
  • 43
  • 66
Anton45
  • 11

1 Answers1

1

QFileSystemModel lists directories on a background thread to avoid blocking the UI. However, once it gets a list of updates in QFileSystemModelPrivate::_q_fileSystemChanged it then fetches the icons for the file in the main thread using QFileInfoGatherer::getInfo() which in turn calls QFileIconProvider::icon(QFileInfo).

You should know Models are not using a different thread. they're using main thread.

If you want to use Model, use a loading animation (loading gif) in your UI to show progressing time.

Farhad
  • 4,119
  • 8
  • 43
  • 66
  • hey, thanks for your answer. Could you give my advice, is it possible to stop updating for some time ? – Anton45 Jul 22 '17 at 08:05
  • @Anton45 no problem my friend. you can use a timer to update UI for example every 5 second. (make sure updating process is less that 1 sec) – Farhad Jul 22 '17 at 08:07
  • I understood idea, but GUI with treeview is updated automatically, I don't do anything with this. Should I add something in MainWindow ? thanks for response – Anton45 Jul 22 '17 at 08:26
  • @Anton45 I just make a simple app with QFileSystemModel and add/delete 200 folders. my UI updated with no freezing. what exactly you do before frezing ? – Farhad Jul 22 '17 at 09:33
  • What do you mean by "use a design a loading (waiting) in your UI to make some time for loading."? – Aykhan Hagverdili Apr 13 '19 at 14:05
  • @Ayxan I update my answer. thank you for your reply. – Farhad Apr 13 '19 at 14:57