首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略qTreeView scrollToBottom()

忽略qTreeView scrollToBottom()
EN

Stack Overflow用户
提问于 2014-12-23 17:09:51
回答 2查看 527关注 0票数 1

欧弗兰!

我正疯狂地尝试在qTreeView (或QListView)小部件中使用qTreeView()。为了简化我的问题,我将代码简化为一个简单的scrollToBottom(),我也无法使用它。以下是mainWindow代码:

代码语言:javascript
复制
#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位。

知道吗?谢谢。我

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-24 13:26:13

QFileSystemModelQFileSystemWatcher是在一个单独的线程中保持最新的。因此,简单地在树视图上设置模型并不能确保在调用scrollToBottom时模型将被完全填充。使用一个带有小延迟的单次定时器来给模型填充时间。

代码语言:javascript
复制
QTimer::singleShot(1000, ui->treeView, SLOT(scrollToBottom()));

此外,(我不知道您的应用程序,所以这可能是真的,也可能不是真的)您的用户可能会感到困惑,他们需要看到的数据无论如何都在视图的底部。您可能会考虑是否可以将视图项按相反顺序排序(因此在顶部有您需要的数据),以避免滚动,并可能使使用更加直观。

票数 0
EN

Stack Overflow用户

发布于 2019-12-16 06:45:14

这是因为QFileSystemModel的异步工作方式,以及在Qt中似乎从未修复过的错误:https://bugreports.qt.io/browse/QTBUG-9326

您可以在调用QApplication::sendPostedEvents()之前执行scrollTo(),但必须在连接到directoryLoaded信号的函数中调用它们:

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27624957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档