Qt World Summit: Register Today!

[Solved]Get QModelindex

  • I got a treeview and a QFileSystemModel.
    Yes it,s a simpel file explorer.

    And I want to show all data about the file like size type etc.etc

    But I also want to show the file and its data in another Textbrowser so when I click on the filename in the Treeview its OK but when I click on other columns in the same row it's not possible to show the file.

    I want to show the file also if I clic on the second column or third or etc etc.
    Here is the code to get the file.

    @void Info::on_fileListTree_doubleClicked(QModelIndex index)

    I know that I need to change the index to the first column in the row but how?

    And another question.

    How could i get the same event as if i Click on the Row when I change row with the arrows on my keyboard.

  • First of all, I think you should at least make sure that you have the right selection mode enabled. You should use row-type selection.

    Then, if you want to respond to a (double) click on a cell in a column other than column 0 as if it were in column 0, you could do something like this:

    //untested code
    void Info::on_fileListTree_doubleClicked(QModelIndex index)
    if (index.column() > 0) {
    //get index for column 0 in the same row
    index = index.model().index(index.row(), 0, index.parent());

  • Yes that's exctly what I wanted and I got the selection mode sat to rowselect.


  • By the way, I think the code becomes easier to read if you rename the index variable to something else. For times index in one line of code is a bit confusing, perhaps ;-)

  • Thanks.
    Do you have an answer to my other question also.

    bq. How could i get the same event as if i Click on the Row when I change row with the arrows on my keyboard.

    I miss the selection/row change or something in the slots

  • Did you try to connect to the itemActivated() signal?

  • Nope didn't find it in the treeview doc:

  • Get the [[Doc:QItemSelectionModel]] with selectionModel(), then connect to one or more of the signals currentChanged, selectionChanged or currentRowChanged.

  • Ok thanks I try this a little later.

Log in to reply