Qt:使用QListView和QFileSystemModel浏览文件系统。如何删除文件夹中的第一项?

Qt:使用QListView和QFileSystemModel浏览文件系统。如何删除文件夹中的第一项?,qt,qlistview,qfilesystemmodel,Qt,Qlistview,Qfilesystemmodel,我在一个没有键盘/鼠标的系统上做主题所说的,所以我需要“从代码”来完成这项工作。当我更改QListView的RootIndex时,我想突出显示第一行 下面是我制作的一个小测试项目的mainwindow.cpp: #include "mainwindow.h" #include "ui_mainwindow.h" #include <QEvent> #include <QKeyEvent> #include <QDebug> #include <QTim

我在一个没有键盘/鼠标的系统上做主题所说的,所以我需要“从代码”来完成这项工作。当我更改QListView的RootIndex时,我想突出显示第一行

下面是我制作的一个小测试项目的mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QEvent>
#include <QKeyEvent>
#include <QDebug>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model = new QFileSystemModel;
    model->setRootPath("/Users/anders/Downloads/Browser");

    listView = new QListView;
    listView->setModel(model);
    listView->show();

    QTimer::singleShot(2000, this, SLOT(LightItUp1()));

}

void MainWindow::LightItUp1()
{
    qDebug("LightItUp1");
    listView->setRootIndex(model->index("/Users/anders/Downloads"));
    listView->setCurrentIndex(model->index(0, 0, listView->rootIndex()));

    QTimer::singleShot(2000, this, SLOT(LightItUp2()));
}

void MainWindow::LightItUp2()
{
    qDebug("LightItUp2");
    listView->setRootIndex(model->index("/Users/anders/Downloads/Browser"));
    listView->setCurrentIndex(model->index(0, 0, listView->rootIndex()));

    QTimer::singleShot(2000, this, SLOT(LightItUp3()));
}


void MainWindow::LightItUp3()
{
    qDebug("LightItUp3");
    listView->setRootIndex(model->index("/Users/anders/Downloads"));
    listView->setCurrentIndex(model->index(0, 0, listView->rootIndex()));

    QTimer::singleShot(2000, this, SLOT(LightItUp4()));
}


void MainWindow::LightItUp4()
{
    QString p = "/Users/anders/Downloads/Mail";
    listView->setRootIndex(model->index(p));
    listView->setCurrentIndex(model->index(0, 0, listView->rootIndex()));
}

MainWindow::~MainWindow()
{
    delete listView;
    delete model;
    delete ui;
}
我认为,当我在模型上设置根路径(rp),然后将视图设置为使用模型时,如果我正确设置索引,视图应该能够在rp的所有子文件夹中移动。我将重新阅读Model/View、QListView和QFileSystemModel上的Qtdocs,但我想发布这篇文章,以防有人知道发生了什么。

我从中得到了一些帮助,以下是我的结论:

为了使QFileSystemModel正常工作,需要运行GUI事件循环。我猜你是因为这个才添加了
QTimer::singleShot(…)
行的吧?然而,你只给了它2秒钟。从以下文件中:

在模型填充目录之前,对rowCount()的调用将返回0

这意味着在构建主窗口后,您有2秒钟的时间来构建其他所有内容,启动GUI事件循环,然后QFileSystemModel填充目录。发生故障的目录是否很大?我想是的

你可以试着给计时器一个更长的时间间隔。更好的解决方案可能是创建一个快捷方式,选择列表中的第一项,如下所示:

QShortcut* sh = new QShortcut(QKeySequence("Ctrl+1"), this);
connect(sh, SIGNAL(activated()), this, SLOT(LightUpFirst()));

LightUpFirst函数进行选择。希望有帮助

我想我现在可以用了。在更改列表的rootIndex之后,我必须等待模型完成它的工作。在从模型中获得directoryLoaded信号之前,我不会在新目录中设置currentIndex。现在我开始工作了。模型中的数据没有排序,因此row=0&col=0毕竟不是列表中的第一项,但这是另一个主题:)

编辑:今晚对这个做了一些修改,并添加了最后的润色

const QString rp = "/home/anders/src";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model = new QFileSystemModel;
    model->setRootPath(rp);

    list = new QListView;
    list->setModel(model);
    list->show();

    connect(model,
            SIGNAL(directoryLoaded(QString)),
            this,
            SLOT(model_directoryLoaded(QString)));

    QTimer::singleShot(2000, this, SLOT(changeRoot()));
}

void MainWindow::model_directoryLoaded(QString path)
{
    qDebug() << "loaded" << path;
    model->sort(0, Qt::AscendingOrder);
    list->setCurrentIndex(model->index(0, 0, list->rootIndex()));
}

void MainWindow::changeRoot()
{
    qDebug() << "changeRoot";
    model->setRootPath(rp + "/trunk");
    list->setRootIndex(model->index(rp + "/trunk"));
}

MainWindow::~MainWindow()
{
    delete list;
    delete model;
    delete ui;
}
const QString rp=“/home/anders/src”;
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
模型=新QFileSystemModel;
模型->设置根路径(rp);
列表=新的QListView;
列表->设置模型(模型);
列表->显示();
连接(模型,
信号(directoryLoaded(QString)),
这
插槽(model_directoryLoaded(QString));
QTimer::singleShot(2000,this,SLOT(changeRoot());
}
void main window::model_directoryLoaded(QString路径)
{
qDebug()setCurrentIndex(模型->索引(0,0,列表->根索引());
}
void MainWindow::changeRoot()
{
qDebug()setRootPath(rp+“/trunk”);
列表->设置根索引(模型->索引(rp+“/trunk”);
}
MainWindow::~MainWindow()
{
删除名单;
删除模型;
删除用户界面;
}

我不太清楚这一切到底是怎么回事。只要开始看看发生了什么,您就可以确保所使用的索引是有效的。在LightItUp4中,do:QModelIndex i=model->index(p);如果(!i.isValid())//那么模型找不到目录,您不应该//更改根索引我认为@anr78正在尝试编写一个自动文件系统浏览器。@buck添加了错误检查,所有索引都是有效的。我也读了那篇文章,这确实是设置计时器的原因。实际上,目录不是很大,即使我将其增加到20秒,它也会失败。在我的实际代码中,从模型开始填充到我尝试在listView上操作的时间可能是几分钟,但它仍然以相同的方式失败。失败的目录的内容是什么?是所有的目录还是所有的文件还是混合的(也许这起到了一定的作用?)?此外,可能是QFileSystemModel没有加载失败的目录。也许当您更改视图的根索引时,您还应该调用dir的内容似乎并不重要。我试图在没有任何运气的情况下设置rootPath(参见示例中的注释),但我怀疑我应该等待QFileSystemModel发出一些信号后再进行操作。当我等待directoryLoaded(QString)时,我设法突出显示某个目录,虽然它不是第一个目录。但是,嘿,它的进展。嗨,你有办法突出显示特定的文件吗?这里发布的代码是最终结果吗?如何在文件之间上下滚动(并在每次选中文件时突出显示)谢谢,RanI不记得这是如何结束的。那是很久以前的事了,代码已经不存在了(也从来没有传到风投那里)。对不起,谢谢,所以你不建议使用这个代码,我明白,对吧?如果它工作的话,它是可用的。我真的不记得了:)
const QString rp = "/home/anders/src";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model = new QFileSystemModel;
    model->setRootPath(rp);

    list = new QListView;
    list->setModel(model);
    list->show();

    connect(model,
            SIGNAL(directoryLoaded(QString)),
            this,
            SLOT(model_directoryLoaded(QString)));

    QTimer::singleShot(2000, this, SLOT(changeRoot()));
}

void MainWindow::model_directoryLoaded(QString path)
{
    qDebug() << "loaded" << path;
    model->sort(0, Qt::AscendingOrder);
    list->setCurrentIndex(model->index(0, 0, list->rootIndex()));
}

void MainWindow::changeRoot()
{
    qDebug() << "changeRoot";
    model->setRootPath(rp + "/trunk");
    list->setRootIndex(model->index(rp + "/trunk"));
}

MainWindow::~MainWindow()
{
    delete list;
    delete model;
    delete ui;
}