0

i am programming a file explorer and I am using a QFileSysteModel as the base. I noticed that the methods QFileSystemModel::index() and QFileSystemModel::fetchMore causing the model to emit the signal rowsInserted.

I have connected the rowsInserted signal to a slot which sends data about new inserted rows. The problem is that the rows which comes from QFileSystemModel::index() and QFileSystemModel::fetchMore are not really new, but added to the model by the QFileSystemModel itself, which causes trouble in my program.

I tried so set flags before using QFileSystemModel::index() and QFileSystemModel::fetchMore but it was not reliable especially with the QFileSystemModel::fetchMore.

Like:

m_blockRowInsert = true; // <-- blocks rowInserted for m_fileSysModel->index
auto index = m_fileSysModel->index(pathNew); // calls immediately rowsInserted
if(index.isValid())
{
    if(m_fileSysModel->canFetchMore(index))
    {
         m_blockRowInsert = true;// <-- does not work reliable because rowsInserted can be called more than once by fetchmore
         m_fileSysModel->fetchMore(index); // <-- calls rowsInserted after completing the function
    }
}

I tried to reset the flag like this:

void onRowsInserted(const QModelIndex &parent, int first, int last)
{
    if(m_blockRowInsert)
    {
        m_blockRowInsert = false;
        return;
    }
}
Maitai
  • 42
  • 1
  • 10
  • I'm a little bit bewildered with your situation, can you just block signals before ```m_fileSysModel->fetchMore(index)``` – qloq Jun 28 '21 at 14:39
  • It would be the same like setting up a flag, because i needed a defined location to unblock. I tried to block signals before, but i did not work since QFileSystemModel works in a different thread. I made it work with connecting a slot to the directoryLoaded signal, where I reset my flag. – Maitai Jun 29 '21 at 05:04

1 Answers1

0

You can use blockSignals function. In that way, you block signals before fetchMore and enable signals after that.

...
if(m_fileSysModel->canFetchMore(index))
{
     this->blockSignals(true);
     m_fileSysModel->fetchMore(index);
     this->blockSignals(false);
}
...

I assumed that this is sender of rowInserted signals.

Ehsan.D
  • 16
  • 2