问题描述:

I have a QTreeView linked to a QFileSystemModel. I also have a QLineEdit with two purposes :

  1. when the user click the QTreeView, the QLineEdit is populate with the item clicked (path & filename if applied)
  2. when the user edit the QLineEdit and press enter, I want to :

    a. synchronize the QTreeView to that position if the path exist

    b. if the path do not exist, I want to repopulate the QLineEdit with the current position of the QTreeView

Everything is working well except when the condition 2a above is met, I want to :

  1. return focus on QTreeView
  2. expand the current folder
  3. resize the 1st column to contents
  4. and scroll the view to that index (scroll to top)

Here is the code I put in place :

QDirectorySelector::QDirectorySelector(QString const & title, QWidget *parent)

: QWidget(parent)

{

mDirectoryModel = new QFileSystemModel;

mDirectoryModel->setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);

mDirectoryModel->setRootPath("");

mDirectoryTitle = new QLabel(title);

mDirectoryView = new QTreeView;

mDirectoryView->setModel(mDirectoryModel);

mDirectoryView->setSortingEnabled(true);

mDirectoryView->sortByColumn(0, Qt::AscendingOrder);

mDirectoryView->setSelectionMode(QAbstractItemView::ExtendedSelection);

mDirectoryEdit = new QLineEdit;

QVBoxLayout * layout = new QVBoxLayout;

layout->addWidget(mDirectoryTitle);

layout->addWidget(mDirectoryView);

layout->addWidget(mDirectoryEdit);

layout->setContentsMargins(0, 0, 0, 0);

setLayout(layout);

connect(mDirectoryView, &QTreeView::clicked, this, &QDirectorySelector::synchronizeEditFromTree);

connect(mDirectoryEdit, &QLineEdit::returnPressed, this, &QDirectorySelector::synchronizeTreeFromEdit);

connect(this, &QDirectorySelector::synchronizationDone, this, &QDirectorySelector::reactToExpansionStep1, Qt::QueuedConnection);

connect(this, &QDirectorySelector::synchronizationStep1Done, this, &QDirectorySelector::reactToExpansionStep2, Qt::QueuedConnection);

}

void QDirectorySelector::synchronizeEditFromTree(QModelIndex const & index)

{

QString directory{ mDirectoryModel->fileInfo(index).absoluteFilePath() };

mDirectoryEdit->setText(directory);

}

void QDirectorySelector::synchronizeTreeFromEdit()

{

if (QDir(mDirectoryEdit->text()).exists()) {

mDirectoryView->setFocus();

mDirectoryView->setCurrentIndex(mDirectoryModel->index(mDirectoryEdit->text()));

mDirectoryView->setExpanded(mDirectoryView->currentIndex(), true);

emit synchronizationDone(mDirectoryView->currentIndex());

} else {

selectDirectory(mDirectoryView->currentIndex());

}

}

void QDirectorySelector::reactToExpansionStep1(QModelIndex const & index)

{

mDirectoryView->resizeColumnToContents(0);

emit synchronizationStep1Done(mDirectoryView->currentIndex());

}

void QDirectorySelector::reactToExpansionStep2(QModelIndex const & index)

{

mDirectoryView->scrollTo(mDirectoryView->currentIndex(), QAbstractItemView::PositionAtTop);

}

If I type an existing path and press enter I got this unexpected behavior :

  • the focus is done (ok)
  • the current index of the view is done correctly (ok)
  • the expension of the sub folder is done (ok)
  • the resize of the 1st column is done only at the path level without including the expended part (not expected)
  • the scroll to is not done at all (not ok)

At this point, if I return to the QLineEdit and press enter again, everything is done correctly. In fact, I observed this pattern, if the specified folder was never "open" (seen/loaded/expended), I get the unexpected behavior. If the specified was "open" (seen/loaded/expended), I get the expected behavior.

I also try to call two times the code in the finishDirectoryEditing without any success. Everything is the same.

Is anyone have any Idea of what is happening here? And better, any suggestion to solve this unexpected behavior.

I think that the QTreeView working with the QFileSystemModel is already too awkward in term of behavior. I mean, it never hide the expansion mark of an empty folder. May be it's possible to change this but I got no success either with the fetch more strategy I found elsewhere on the net.

Thanks to all!

相关阅读:
Top