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. QStandardItem subclassing?
QtWS25 Last Chance

QStandardItem subclassing?

Scheduled Pinned Locked Moved General and Desktop
24 Posts 6 Posters 11.9k 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
    ManasQt
    wrote on last edited by
    #1

    hi,
    can any one tell me with a sample code on how to subclass QStandardItem in order to use QStandardItemModel,QtreeView.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      May I ask you what did you try and what didn't work? (And why do you want to subclass QStandardItem at all?)

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • M Offline
        M Offline
        manjuesco
        wrote on last edited by
        #3

        what did you try and what didn’t work?

        [url=http://femaleescortagency.net/category/punjabi-escorts/]Punjabi Escort [/url]

        1 Reply Last reply
        0
        • M Offline
          M Offline
          ManasQt
          wrote on last edited by
          #4

          hi all,
          never mind i got it.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Beka
            wrote on last edited by
            #5

            Hi, can you please put here a simply code? because i have the same problem as you had

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              How do you know? ManasQt didn't actually describe his problem, nor his solution. I guess the same applies to you as to him: what did you try, and what did not work?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                Hi Beka,

                The post is more than 1 year old. So for the solution you can post your code.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Beka
                  wrote on last edited by
                  #8

                  I need to override method of QStandardItem operator<(), but thats not the problem, problem is i dont know how to use myItem in models

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Did you try just adding your own item instance instead of the standard one? What happened?

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      Beka
                      wrote on last edited by
                      #10

                      I have subbclassed QStandardItemModel already, so I just try to set in constructor something like this: this->setItemPrototype(new myItem());

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        You normally should not need to subclass QStandardItemModel itself, you should be able to use it without subclassing.

                        Anyway, your call to setItemPrototype should work. However, did you think to reimplement the QStandardItem::clone() method to return an instance of your own class?

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          Beka
                          wrote on last edited by
                          #12

                          Yes, I did it, and i debug if it is called, and it works, but my method operator<() is never called

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            Beka
                            wrote on last edited by
                            #13

                            I just tried to create two new myItems and compare them, it works, but thats predictable
                            but when I called for example myModel->item(r,c)->operator <(other myItem) it isnt called

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

                              Could you show us the signature for that function from your header? Did you happen to forget the const perhaps?

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                Beka
                                wrote on last edited by
                                #15

                                @ bool operator <(const myItem &other) const;
                                virtual myItem *clone() const;@

                                and, after i setItemPrototype, will it take care of retrieving myItem insted of QStandardItem? or i must reimplement or functions which i am working with?

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #16

                                  Are you sure about that space between operator < ?

                                  And yes, thanks to polymorphism, your own function should be called if it actually is an object of your own type. How do you actually set your items? The prototype is just for editing and things like that. How do you fill your model?

                                  1 Reply Last reply
                                  0
                                  • B Offline
                                    B Offline
                                    Beka
                                    wrote on last edited by
                                    #17

                                    no, i delete it

                                    i fill it with functions setItem or setData

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on last edited by
                                      #18

                                      Show, not tell. It is hard to figure out where the mistake in your code is if you don't show your actual code...

                                      1 Reply Last reply
                                      0
                                      • B Offline
                                        B Offline
                                        Beka
                                        wrote on last edited by
                                        #19

                                        yes i know, but the code is really big...
                                        @
                                        myModel->setItem( row, column, new myItem(QIcon(icon), "some text") );
                                        myModel->setData( myModel->index(row, column), "text" );
                                        @

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          andre
                                          wrote on last edited by
                                          #20

                                          So, extract the relevant sections yourself, or create a small, self-contained example that shows the problem. Also, when posting code, please use @ tags around your code section (I already updated your post in this case) to improve readability.

                                          Your code, as far as I can oversee it, looks fine to me. I have no idea why it should fail.

                                          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