Results 1 to 13 of 13

Thread: Subclasing QAbstractItemModel Issue

  1. #1
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Subclasing QAbstractItemModel Issue

    Hi,
    im working on one application and I need to subclass QAbstractItemModel. I googled which methods I need to reimplement to make it work. Basic Idea is I need to reimplement mainly data methode (the others just to do the same as usualy), to choose what is returned. Problem is, if I return anything else than simple QVariant(), for example if I return 1 (I also tryed conversion of everything I tryed straight to QVariant befor returning) result looks like this:

    Im not able to figure out from where these checkboxes appeared there. I can return anything, and still, here they are. Its different when I return like 0 or 1. On 0 its unchecked on 1 its checked. But still, I cant figure it out.

    I know maybe I just forgot something basic, or Im just blind to see the problem and it can be something realy simple, but I tryed for some time and Im out of ideas.
    Can please anybody help me with this? I would realy appreciate any help.

    PS: I put this class in simple project, sou if needed, you can try to run it and see what it is doing.
    Attached Files Attached Files
    Last edited by wolfi3b; 23rd September 2010 at 12:34.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subclasing QAbstractItemModel Issue

    The check boxes will be shown by the standard delegate if the flags() method returns Qt::ItemIsUserCheckable (possibly also Qt::ItemIsTristate) among the flags. It also seems that if data() returns anything other than an empty QVariant() for the Qt::CheckStateRole the check boxes are displayed also. Your implementation of data() does not distinguish on role and returns QVariant(1) for all roles.

  3. The following user says thank you to ChrisW67 for this useful post:

    wolfi3b (24th September 2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclasing QAbstractItemModel Issue

    Thank you for your answer. I lknow its returning 1 all time, I meant it just like example. What I didnt know was, why these check boxes will be there. Can I ask you where exactly did you get this info that these checkboxes are there by default? Can you post here a link? Im goint to try it right now, but still Im quite disapointed I didnt found it by myself in documentation

    EDIT: Ok, the problem was realy the flags and role. Thank you for help and plesase if its possible can you still put here that link? I know I have to be blind. I suppose its gona be in bold somewhere nobody can miss it
    Last edited by wolfi3b; 24th September 2010 at 18:39.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Subclasing QAbstractItemModel Issue

    The link is called "experience".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclasing QAbstractItemModel Issue

    Nice, I hope I can get this source as well :-)
    One more question. Lets say I want to set all cells content to specific picture using this methode. Again its just example to figure out how exactly its working. I tryed it like this:
    Qt Code:
    1. QVariant TabsModel::data ( const QModelIndex & index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. else if(role == Qt::DisplayRole)
    7. {
    8. QPixmap img("C:/Tab/box.png");;
    9. return QVariant::fromValue(img); // I tryed straight return img; as well
    10. }
    11. else return QVariant();
    12. }
    To copy to clipboard, switch view to plain text mode 

    There is no warning no error. but problem is when window show, there is no picture in any cell.

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Subclasing QAbstractItemModel Issue

    I guess your problem is more on the receiver side than on the sender:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. QVariant image()
    4. {
    5. return QPixmap("/usr/share/httpd/icons/tex.png");
    6. }
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. QVariant v = image();
    13. QPixmap p = v.value<QPixmap>();
    14. QLabel l;
    15. l.setPixmap(p);
    16. l.show();
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Subclasing QAbstractItemModel Issue

    You should read the docs on the model-view approach and especially the part where they say about data roles.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclasing QAbstractItemModel Issue

    On reciever side? Hmm...Its reimplementing data methode, Im not sure what probelme on reciever side it can be....I thought if I do it this way, using should be just like normal. Or I have to change something more than I did?

  10. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Subclasing QAbstractItemModel Issue

    Sorry I did not read careful enough and thought - since returning a pixmap as a QVariant is just fine - you have trouble to convert the variant back and so you are unable to paint the pixmap.

  11. #10
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclasing QAbstractItemModel Issue

    hmmm...but problem? I just read about subclasing to check it from here
    http://doc.qt.nokia.com/4.6/model-vi...ng-models.html

    As I understand, it should be enough to create methods which I have. I supposed if I created data methode, AbstractItemModel should be able to work with default mehtods for finish it.

  12. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subclasing QAbstractItemModel Issue

    Quote Originally Posted by wysota View Post
    The link is called "experience".
    I used the Suck-it-and-see design pattern with this one

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Subclasing QAbstractItemModel Issue

    I'll be gentle and I will stuff the answer right in front of your nose this time: you are using a wrong role.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclasing QAbstractItemModel Issue

    Thanks your for patience. I just figured it out and its working now. I know sometimes Im little bit blockheaded(hope its the right term in english ).

    One more time thank you guys, you didnt give up with me.

Similar Threads

  1. subclasing a Qlist?
    By Ricardo_arg in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2010, 08:40
  2. Once more : QAbstractItemModel woes
    By Valheru in forum Qt Programming
    Replies: 10
    Last Post: 15th January 2008, 10:44
  3. Checkboxes in QAbstractITemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th November 2007, 20:23
  4. QAbstractItemModel shifiting up and down
    By mattjgalloway in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2007, 08:41
  5. QAbstractItemModel for dummies
    By morty in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 15:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.