欧弗兰!
我正疯狂地尝试在qTreeView (或QListView)小部件中使用qTreeView()。为了简化我的问题,我将代码简化为一个简单的scrollToBottom(),我也无法使用它。以下是mainWindow代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <qfilesystemmodel.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFileSystemModel *model = new QFileSystemModel(this);
QModelIndex modelRootIndex = model->setRootPath(QDir::rootPath());
ui->treeView->setModel(model);
ui->treeView->setRootIndex(modelRootIndex);
ui->treeView->scrollToBottom();
if(modelRootIndex.isValid()) std::cout << "validIndex" << std::endl;
}
MainWindow::~MainWindow()
{
delete ui;
}据我所知,这一切都没问题(我在标准输出中得到了"ValidIndex“字符串),但小部件根本没有滚动到底部。
我使用的是桌面QT5.0.2 msvc2010 32位。
知道吗?谢谢。我
发布于 2014-12-24 13:26:13
QFileSystemModel和QFileSystemWatcher是在一个单独的线程中保持最新的。因此,简单地在树视图上设置模型并不能确保在调用scrollToBottom时模型将被完全填充。使用一个带有小延迟的单次定时器来给模型填充时间。
QTimer::singleShot(1000, ui->treeView, SLOT(scrollToBottom()));此外,(我不知道您的应用程序,所以这可能是真的,也可能不是真的)您的用户可能会感到困惑,他们需要看到的数据无论如何都在视图的底部。您可能会考虑是否可以将视图项按相反顺序排序(因此在顶部有您需要的数据),以避免滚动,并可能使使用更加直观。
发布于 2019-12-16 06:45:14
这是因为QFileSystemModel的异步工作方式,以及在Qt中似乎从未修复过的错误:https://bugreports.qt.io/browse/QTBUG-9326。
您可以在调用QApplication::sendPostedEvents()之前执行scrollTo(),但必须在连接到directoryLoaded信号的函数中调用它们:
MyFileBrowser::MyFileBrowser(QWidget *parent) : QWidget(parent), ui(new Ui::MyFileBrowser) {
//...
connect(model, SIGNAL(directoryLoaded(QString)), this, SLOT(dirLoaded(QString)));
QModelIndex folderIndex = model->index("path/to/dir");
files->setCurrentIndex(folderIndex);
files->expand(folderIndex);
}
void WFileBrowser::dirLoaded(QString dir) {
if (dir == model->filePath(files->currentIndex())) {
QApplication::sendPostedEvents(); // booyah!!
files->scrollTo(files->currentIndex(), QAbstractItemView::PositionAtTop);
}
}https://stackoverflow.com/questions/27624957
复制相似问题