anr*_*r78 5 qt qlistview qfilesystemmodel
我在没有键盘/鼠标的系统上做主题所说的事情,所以我需要“从代码”完成这项工作。当我更改 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;
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,LightItUp 1-3 做我想要的,但 LightItUp4 没有。如果我交换 2 和 4 中的文件夹,它们都无法执行我想要的操作,而 1 和 3 仍然可以工作。我怀疑我对如何使用这个模型/视图有一些误解,但不知道是什么。
编辑:创建了一个更简单的示例,其中提到了错误检查 @buck。查看源代码中的注释。
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); //using model->setRootPath(rp + "/trunk") instead works
listView = new QListView;
listView->setModel(model);
listView->show();
QTimer::singleShot(2000, this, SLOT(LightItUp1()));
}
void MainWindow::LightItUp1()
{
qDebug("LightItUp1");
QModelIndex p = model->index(rp + "/trunk");
if (!p.isValid()) {
qDebug("index not valid\n");
return;
}
//model->setRootPath(rp + "/trunk") here does not make it work
listView->setRootIndex(p);
listView->setCurrentIndex(model->index(0, 0, p));
}
Run Code Online (Sandbox Code Playgroud)
我认为当我在模型上执行 setRootPath(rp) ,然后设置视图以使用模型时,如果我正确设置索引,视图应该能够在 rp 的所有子文件夹中移动。我将重新阅读关于模型/视图、QListView 和 QFileSystemModel 的 Qtdocs,但我想发布这个以防有人了解正在发生的事情。
我想我现在已经可以使用了。更改列表的 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11627 次 |
| 最近记录: |