How can I prevent rowsInserted signal from QFileSystemModel in special cases?

178 views Asked by At

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;
    }
}
1

There are 1 answers

0
Ehsan.D On

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.