[SOLVED] Check if a line is selected from a QTableView



  • I use a QSqlTableModel with a QTableView, named model and view respectively. I have some buttons that I would like to become active when the user had actually selected a line from view. How can this be done? I thought of checking the value of

    @view->currentIndex().row()@

    I noticed that if it would be -1, it would be the case that no selection is made. However, sometimes with no selection been made, this value seems to become 0, which should be the case if someone would choose the first record of the view!

    Any suggestions? Once again, I am looking for a way to check if the user had choosen any line from the view.

    Updated: changed subject to "solved", since the initial problem was solved.



  • Try
    @view->currentIndex().isValid()@



  • [quote author="loladiro" date="1307303899"]Try
    @view->currentIndex().isValid()@[/quote]

    Thank you! However, it behaves strange! I do not select anything and the first time it seems to return false, I try a second time and it returns true! (both times: no selection)

    If it has any importance, I am using a Mac.



  • [quote author="Stavros" date="1307304591"]
    [quote author="loladiro" date="1307303899"]Try
    @view->currentIndex().isValid()@[/quote]

    Thank you! However, it behaves strange! I do not select anything and the first time it seems to return false, I try a second time and it returns true! (both times: no selection)

    If it has any importance, I am using a Mac.[/quote]

    Well, I tried it with a PC (same code!) and I do not have this problem there! Strange!!!



  • Very strange! Do you have any other code modifying the TableView. Otherwise you could try a different model with the same TableView and see how it behaves. That would narrow it down to either the model or the widget.



  • [quote author="loladiro" date="1307305225"]Very strange! Do you have any other code modifying the TableView. Otherwise you could try a different model with the same TableView and see how it behaves. That would narrow it down to either the model or the widget.[/quote]

    I would check these things, but the strange think is that EXACTLY the same code works as it should with a PC but not with a Mac!



  • I don't have a Mac, so I can't help you with that, but you could file a bug report if you think it's a problem with Qt.



  • [quote author="loladiro" date="1307305716"]I don't have a Mac, so I can't help you with that, but you could file a bug report if you think it's a problem with Qt.[/quote]

    Yes, I would make some additional tests first!



  • Good luck, I would be interest in what you find (just for future reference, if I ever develop for Mac)



  • [quote author="loladiro" date="1307306005"]Good luck, I would be interest in what you find (just for future reference, if I ever develop for Mac)[/quote]

    Of course! :-)



  • It would be interesting if a Mac user could report to us if he/she has a similar problem, that is:

    @view->currentIndex().isValid()@

    not giving (ALWAYS!) correct results.



  • I would rather assume that the method returns something else than you think it does. While Qt is not bug free, I think such an obvious issue would have a good likelihood of biting somebody else in the ass before it did you, don't you?

    The current item isn't the same as the selected item, AFAIK. You interact with the selection via the selectionModel method of your view. It returns a QItemSelectionModel.



  • [quote author="Andre" date="1307344471"]I would rather assume that the method returns something else than you think it does. While Qt is not bug free, I think such an obvious issue would have a good likelihood of biting somebody else in the ass before it did you, don't you?

    The current item isn't the same as the selected item, AFAIK. You interact with the selection via the selectionModel method of your view. It returns a QItemSelectionModel.
    [/quote]

    To begin with, I would like to thank @Andre for his valuable answer. :-)

    Following the essence of the answer, I substituted

    @if ( !( view->currentIndex().isValid() ) ) {
    // code to execute when no selection is made
    }@

    with

    @if ( !( view->selectionModel()->isSelected(view->currentIndex()) ) ) {
    // code to execute when no selection is made
    }@

    and it actually works as it should!

    Having said that, I have to point out (again) the fact that the code-in-question

    @if ( !( view->currentIndex().isValid() ) ) {
    // code to execute when no selection is made
    }@

    worked as I assumed it should work at MS-Windows and not like that at my Mac. That is, it SEEMS that (no hard evidence for that, just a hypothesis I make after testing the same project with MS-Windows and Mac) view's current item is the selected item at Qt-SDK for MS-Windows (and if no item is selected, then the current item is invalid), whereas view's current item could be an invalid one or the first one if no selection is made at Qt-SDK for Mac. Maybe I am missing something here, or maybe it is something with my installation(s) of the Qt-SDK, who knows! Just wondering... ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.