Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there any way I get current QModelIndex is Selected
Qt 6.11 is out! See what's new in the release blog

Is there any way I get current QModelIndex is Selected

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    In inside class object of QAbstractItemModel , i have a QModelIndex ,and is there any method which can tell the QModelIndex is selected in the View

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      No, the model should never know about the view. It's quite evident when you think that you can have multiple views based on the same model.

      If you feel this need your design is wrong and you are doing in the model something that should be done elsewhere

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Q 1 Reply Last reply
      0
      • VRoninV VRonin

        No, the model should never know about the view. It's quite evident when you think that you can have multiple views based on the same model.

        If you feel this need your design is wrong and you are doing in the model something that should be done elsewhere

        Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        what is correct way to get the current selection in View , if am inside QAbstractItemModel

        raven-worxR 1 Reply Last reply
        0
        • Q Qt Enthusiast

          what is correct way to get the current selection in View , if am inside QAbstractItemModel

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Qt-Enthusiast said in Is there any way I get current QModelIndex is Selected:

          what is correct way to get the current selection in View , if am inside QAbstractItemModel

          non via Qt API for the reason @VRonin mentioned.
          But if you have to you need to pass a pointer to the view to the model and get it's selection model of it. Then compare the indexes.

          But i am curious what you are trying to achieve?
          If it's about some visual representation then it should probably be handled in an custom item delegate. There you have all the info you need already.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          Q 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @Qt-Enthusiast said in Is there any way I get current QModelIndex is Selected:

            what is correct way to get the current selection in View , if am inside QAbstractItemModel

            non via Qt API for the reason @VRonin mentioned.
            But if you have to you need to pass a pointer to the view to the model and get it's selection model of it. Then compare the indexes.

            But i am curious what you are trying to achieve?
            If it's about some visual representation then it should probably be handled in an custom item delegate. There you have all the info you need already.

            Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            Any example code for that

            raven-worxR 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html

              Could you explain what you are trying to do?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • Q Qt Enthusiast

                Any example code for that

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @Qt-Enthusiast
                I think you know how to pass a pointer to your class.
                So you just need to get the selection model. And this can be very easily looked up in the docs. And when you look at the selection model docs you will know which method to use.

                But still you haven't told what you are trying to do.
                Starting to go the wrong way here may lead to other problems in the future...

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by
                  #8

                  I want to check if the ::data(index)

                  if the index is selected in the view (QtreeView)

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    Yes, but why?

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved