QTreeView只显示指定驱动器及其目录,隐藏所有兄弟节点

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

    model = new QFileSystemModel;
    //model->setRootPath("/");
    model->setRootPath("d:\\");

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

    QModelIndex root = model->index("d:\\", 0);

    for (int i=67; i<=90; i++) {
        QChar ch(i);
        QString str(ch);
        str+=":\\";
        qDebug() << str;
        QModelIndex node = model->index(str, 0);
        if (node==root) continue;
        for (int j=1; j<26; j++) {
            //listView->setRowHidden(0, node.parent(), true); // OK
            listView->setRowHidden(j, node.parent(), true); // OK
        }
    }
}

参考:http://bbs.csdn.net/topics/390995283?page=1#post-398965294

我不明白的是,为什么还要写一个for j的内循环。另外使用root.sibling(i,0);得到的节点好像也不起作用。

posted @ 2015-03-11 17:46  findumars  Views(2679)  Comments(1Edit  收藏  举报