Results 1 to 4 of 4

Thread: Highlight/activate first item in a QListView from code.

  1. #1
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Highlight/activate first item in a QListView from code.

    I'm using QListView and QFileSystemModel to display contents of some folders. When I use my keyboard or post QKeyEvents I can highlight items and move around like I want, but how can I do the same operations from code?

    I want the first item in the rootIndex to be highlighted by default (like it is if I send Key_Home to the list), and to be able to move up and down in the list with function calls. I suspect maybe I can use setCurrentIndex to do the latter, if I find out how to do the first.

  2. #2
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Highlight/activate first item in a QListView from code.

    Quote Originally Posted by anr78 View Post
    I'm using QListView and QFileSystemModel to display contents of some folders. When I use my keyboard or post QKeyEvents I can highlight items and move around like I want, but how can I do the same operations from code?

    I want the first item in the rootIndex to be highlighted by default (like it is if I send Key_Home to the list), and to be able to move up and down in the list with function calls. I suspect maybe I can use setCurrentIndex to do the latter, if I find out how to do the first.
    Qt Code:
    1. QModelIndex index = model->createIndex( row, column );
    2. if ( index.isValid() )
    3. model->selectionModel()->select( index, QItemSelectionModel::Select );
    To copy to clipboard, switch view to plain text mode 
    Dong Back Kim

  3. #3
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Highlight/activate first item in a QListView from code.

    Thanks. Trying to get this into my code now, with a couple of issues. model->createIndex is protected, so I'm trying to find another way to create the index. And I think model->selectionModel... should be list->selectionModel...

  4. #4
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Highlight/activate first item in a QListView from code.

    Been fiddling a bit more with this, and there seems to be something about this Model/View I don't understand. Below is the mainwindow.cpp from a small testproject I have made. The aim is to display folders from the model in my QListView, and higlight the second row. When the first timer goes off, LightItUp() does just that. When the second timer goes off, LightItUp2() opens the folder I want, but does not highlight any items. Am I not changing folders/indexes the way I'm supposed to?

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QEvent>
    5. #include <QKeyEvent>
    6. #include <QDebug>
    7. #include <QTimer>
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14.  
    15. model = new QFileSystemModel;
    16. model->setRootPath("/Users/anders/Downloads");
    17.  
    18. list = new QListView;
    19. list->setModel(model);
    20. list->show();
    21.  
    22. QTimer::singleShot(3000, this, SLOT(LightItUp()));
    23.  
    24. }
    25.  
    26. void MainWindow::LightItUp()
    27. {
    28. qDebug("LightItUp");
    29. list->setRootIndex(model->index(model->rootPath()));
    30. list->setCurrentIndex(model->index(1, 0, list->rootIndex()));
    31.  
    32. QTimer::singleShot(3000, this, SLOT(LightItUp2()));
    33. }
    34.  
    35. void MainWindow::LightItUp2()
    36. {
    37. qDebug("LightItUp2");
    38. list->setRootIndex(model->index("/Users/anders/Downloads/Browser"));
    39. list->setCurrentIndex(model->index(1, 0, list->rootIndex()));
    40. }
    41.  
    42.  
    43. MainWindow::~MainWindow()
    44. {
    45. delete ui;
    46. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Highlight Tree Item Viw
    By sajis997 in forum Qt Programming
    Replies: 4
    Last Post: 19th July 2011, 09:40
  2. Graphics item highlight
    By mukunda in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2011, 18:21
  3. Menu Item has no highlight
    By blackfox in forum Qt Programming
    Replies: 6
    Last Post: 24th September 2008, 12:17
  4. Replies: 5
    Last Post: 6th December 2007, 14:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.