让QTreeView默认选中第一行?

weixin_38049070 2019-09-19 02:06:46
QT 4.7.3中的examples/itemviews/dirview例子中,怎么才能让QTreeView默认选中第一行?即程序运行后是图1的效果: 必须按一下Home键或者方向才能获得图2的效果: 我想程序一运行后,就是图2的效果,怎么实现?谢谢!
...全文
1465 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
C++ Super Man 2022-05-18
  • 打赏
  • 举报
回复

用finditems先找到那个index,然后用setcurrentindex选中,然后用scrolltoindex让界面滚动到该行

xjz1994 2022-03-10
  • 打赏
  • 举报
回复

怎么默认选中多行啊

斜阳日落 2021-08-19
  • 打赏
  • 举报
回复

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)));
tree.setAnimated(false);
tree.setIndentation(20);
tree.setSortingEnabled(true);
tree.setAlternatingRowColors(true);
tree.setWindowTitle(QObject::tr("Dir View"));
tree.resize(640, 480);

      tree.selectionModel()->setCurrentIndex(0,0);
    tree.show();   
斜阳日落 2021-08-19
  • 打赏
  • 举报
回复

setCurrentIndex

weixin_38067396 2019-09-19
  • 打赏
  • 举报
回复
以下代碼在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]
weixin_38070905 2019-09-19
  • 打赏
  • 举报
回复
楼上的兄弟的代码确实可以实现选中第一行。但是,刚打开的时候,只是有一个根,不会像我的打开时,根底下的第一级目录都已经列出来了。其实就是多了:    QString dir("/");    tree.setRootIndex(model.index((dir)));这两行代码。
weixin_38091368 2019-09-19
  • 打赏
  • 举报
回复
木有人能解决这个问题哦。哎~~
weixin_38104476 2019-09-19
  • 打赏
  • 举报
回复
发送个信号模拟选中的操作
weixin_38121538 2019-09-19
  • 打赏
  • 举报
回复
帮顶
weixin_38054042 2019-09-19
  • 打赏
  • 举报
回复
试了,还是不行。贴个代码各位大虾帮忙分析一下。谢谢。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();}
weixin_38062058 2019-09-19
  • 打赏
  • 举报
回复
試試這個行不行, 我在windows7下試了可以.tree.setCurrentIndex(model.index(0,0,model.index(0,0,model.index(QDir::currentPath()))));
weixin_38066102 2019-09-19
  • 打赏
  • 举报
回复
谢谢大家的指教,还是不行。调试了一下:貌似 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);

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧