• 13973阅读
  • 12回复

让QTreeView默认选中第一行? [复制链接]

上一主题 下一主题
离线heha
 
只看楼主 倒序阅读 楼主  发表于: 2011-07-25
QT 4.7.3中的examples/itemviews/dirview例子中,怎么才能让QTreeView默认选中第一行?
即程序运行后是图1的效果:


必须按一下Home键或者方向才能获得图2的效果:




我想程序一运行后,就是图2的效果,怎么实现?谢谢!
离线wxj120bw

只看该作者 1楼 发表于: 2011-07-25
回 楼主(heha) 的帖子
考虑下setCurrentIndex
离线122dddfg
只看该作者 2楼 发表于: 2011-07-26
Re:回 楼主(heha) 的帖子
引用第1楼wxj120bw于2011-07-25 23:16发表的 回 楼主(heha) 的帖子 :
考虑下setCurrentIndex  


离线heha
只看该作者 3楼 发表于: 2011-07-27
试了,还是不行。贴个代码各位大虾帮忙分析一下。谢谢。

int main(int argc, char *argv[])
{  
    QApplication app(argc, argv);  
    
    QFileSystemModel model;
    model.setRootPath("");
    QTreeView tree;
    tree.setModel(&model);                  
    QString dir("./dir");
    tree.setRootIndex(model.index((dir)));
                              
    // Demonstrating look and feel features
    tree.setAnimated(false);
    tree.setIndentation(20);
    tree.setSortingEnabled(true);
    
    tree.setAlternatingRowColors(true);
        
    tree.setWindowTitle(QObject::tr("Dir View"));
    tree.resize(640, 480);
        
    QModelIndex current = tree.currentIndex();  
    tree.selectionModel()->setCurrentIndex(current, QItemSelectionModel::SelectCurrent);
    tree.show();    

    return app.exec();
}
离线alexltr

只看该作者 4楼 发表于: 2011-07-27
試試這個行不行, 我在windows7下試了可以.

tree.setCurrentIndex(model.index(0,0,model.index(0,0,model.index(QDir::currentPath()))));
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线heha
只看该作者 5楼 发表于: 2011-07-27
谢谢大家的指教,还是不行。
调试了一下:貌似 QModelIndex current = tree.currentIndex(); 返回的是QModelIndex(-1,-1,0x0,QObject(0x0) )。所以setCurrentIndex(current, QItemSelectionModel::SelectCurrent);就没用了。
怎么才能获得“aa"这项的index呢?

借用alexltr的方法,改进一下,也是不行的。

    QModelIndex current = tree.rootIndex();
    qDebug() << current;
    QString path = model.filePath(current);
    qDebug() << "a::::"<<path;

    path += "/aa";
   QModelIndex cc = model.index(0, 0, current);
//    QModelIndex cc = model.index(path, 0);
    qDebug() << cc;
    path = model.filePath(cc);
    qDebug() << "a::::"<<path;

    tree.setCurrentIndex(cc);
离线alexltr

只看该作者 6楼 发表于: 2011-07-27
以下代碼在windows xp + Qt4.7.1下OK.

[code/]
#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QFileSystemModel model;
    model.setRootPath("");
    QTreeView tree;
    tree.setModel(&model);
    tree.setCurrentIndex(model.index(0,0,model.index(0,0,model.index(QDir::currentPath()))));

    tree.setWindowTitle(QObject::tr("Dir View"));
    tree.resize(640, 480);
    tree.show();

    return app.exec();
}
[/code]
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线heha
只看该作者 7楼 发表于: 2011-07-27
楼上的兄弟的代码确实可以实现选中第一行。但是,刚打开的时候,只是有一个根,不会像我的打开时,根底下的第一级目录都已经列出来了。其实就是多了:
    QString dir("/");
    tree.setRootIndex(model.index((dir)));

这两行代码。
离线alexltr

只看该作者 8楼 发表于: 2011-07-27
回 7楼(heha) 的帖子
不明白你的意思。能把你的要求说的明白一点吗?
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线heha
只看该作者 9楼 发表于: 2011-07-28
回 8楼(alexltr) 的帖子
就是说,你复制我第三楼的那段代码,然后直接覆盖你的代码。编译后,跑一下,会发现刚启动时没有选中任何的行。
就是想解决一下这个问题。谢谢你的关注哦。
离线heha
只看该作者 10楼 发表于: 2011-08-02
木有人能解决这个问题哦。哎~~
离线henrysnoopy
只看该作者 11楼 发表于: 2011-08-30
发送个信号模拟选中的操作
离线蠢蠢欲懂
只看该作者 12楼 发表于: 2012-05-07
帮顶
快速回复
限100 字节
 
上一个 下一个