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. Custom role for a custom model
QtWS25 Last Chance

Custom role for a custom model

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 9.2k Views
  • 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.
  • M Offline
    M Offline
    Maser
    wrote on last edited by
    #1

    Hi.
    I need a custom role for my models model::data(). Are there any or can i create some?

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

      you can use Qt::UserRole and any number above, for example Qt::UserRole+1 and so on

      "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
      4
      • M Offline
        M Offline
        Maser
        wrote on last edited by Maser
        #3

        Just add a number? Cant i define Qt::UserRoleTwo or something like that?

        VRoninV 1 Reply Last reply
        0
        • M Maser

          Just add a number? Cant i define Qt::UserRoleTwo or something like that?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Maser said:

          Just add a number? Cant i define Qt::UserRoleTwo or something like that?

          Sure, what you normally do is something like this:

          enum MyRoles{
          myRole1=Qt::UserRole
          ,myRole2
          ,myRole3
          // and so on
          };
          

          "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
          3
          • M Offline
            M Offline
            Maser
            wrote on last edited by
            #5

            You mean it doesnt have to be in the Qt:: namespace?

            mrjjM 1 Reply Last reply
            -1
            • M Maser

              You mean it doesnt have to be in the Qt:: namespace?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Maser
              Nope, its just a start value.
              So internal roles ID is < Qt::UserRole

              1 Reply Last reply
              3
              • M Offline
                M Offline
                Maser
                wrote on last edited by
                #7

                How you mean internalrole. I want it passed between classes, so it kinda have to be global.

                miclandM 1 Reply Last reply
                0
                • M Maser

                  How you mean internalrole. I want it passed between classes, so it kinda have to be global.

                  miclandM Offline
                  miclandM Offline
                  micland
                  wrote on last edited by
                  #8

                  @Maser

                  "internal roles" are the roles already predefined by Qt (Qt::ItemDataRole).

                  As you see in the API the data(...) method treats the role as an int so you're free to specify it as enum, as #define or as magic number - wherever you want. As long as you use values greater or equal Qt::UserRole ot prevent conflicts (but the prefered way is to specify an enum, see @VRonin).

                  Depending on what you want to to with the values associated with your roles you have to extend your view so your roles must be at least so "global" that they are known by the model and by the corresponding view.

                  1 Reply Last reply
                  3

                  • Login

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