Results 1 to 20 of 20

Thread: Help with getting my custom model working

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Thanks for the replies, cause I've really been struggling with this. I think I have reimplemented all needed functions. Here's whats in my header file so far and that I have defined in my source file.
    Qt Code:
    1. ConnectionListModel(QObject *parent = 0);
    2. ~ConnectionListModel();
    3.  
    4. //Required items to make this model work
    5. int rowCount(const QModelIndex &index = QModelIndex()) const;
    6. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    7.  
    8. //required items to be able to edit items
    9. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    10. Qt::ItemFlags flags(const QModelIndex &index) const;
    11.  
    12. //Required to be able to resize the model (add & remove shit)
    13. bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
    14. bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
    15.  
    16. private:
    17. QList<connectionData> connectionList;
    To copy to clipboard, switch view to plain text mode 
    One thing that I noticed when stepping through my code while trying to add an item was that I'm never calling my setData funciton with a valid index, so it always skips over the code and never emits the signal that the data changed.

    So now my question is how do I get a valid index? I thought that calling index(0) would give me the index for the item that I just inserted at the front of the list, but that isn't the case.

    Here's the calling code
    Qt Code:
    1. //where m_pSessoinModel is a connectionlistmodel
    2. ConnectionListModel::connectionData addMe;
    3.  
    4. m_pSessionModel->insertRows(0, 1); //inserting 1 row before row #0
    5. m_pSessionModel->setData( m_pSessionModel->index(0), &addMe);
    To copy to clipboard, switch view to plain text mode 

    Any ideas? The examples online are not very helpful here.
    Paul

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Quote Originally Posted by thomaspu View Post
    So now my question is how do I get a valid index? I thought that calling index(0) would give me the index for the item that I just inserted at the front of the list, but that isn't the case.
    index(0) shouldn't even compile AFAIK... it should be index(0, 0) (or index(0, 1) and so on...). An index obtained through the index() method represents a single cell, not a whole row...

    besides, your header is slightly wrong... The default role for the setData() function should be Qt:isplayRole as well or am I missing something?
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    The QAbstractListModel defines a default for the column:
    Qt Code:
    1. virtual QModelIndex index ( int row, int column = 0, const QModelIndex & parent = QModelIndex() ) const
    To copy to clipboard, switch view to plain text mode 
    But when do you ever get a valid QModelIndex? When I'm calling the insertRows function, I don't know the parent at all. The description in the function reads
    Returns the index of the data in row and column with parent.
    So If I never have the parent... thats probably why it doesn't work?

    Yeah, the role should have just been Qt:isplayRole

    Paul

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Quote Originally Posted by thomaspu View Post
    When I'm calling the insertRows function, I don't know the parent at all. The description in the function reads So If I never have the parent... thats probably why it doesn't work?l
    The point is that you never HAVE any parents anywhere since you're using a table and not a hierarchical (i.e. tree) model. When no parent is involved the parent is the header node which is represented by an invalid parent (which is the default parent passed to all methods that require a parent BTW... )
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Ok, that makes sense since this is just a flat model. So any ideas why nothing shows up on my QListView after I add an item to my model?

    Paul

  6. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting my custom model working

    May I ask for the full code?
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #7
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Sure, let me put it together. Just a few min...

  8. #8
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Ok, all you need is attached.

    Paul
    Attached Files Attached Files

  9. #9
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Ok I found the bug... You messed up the rowCount() function... The point in this one is to return the number of connections when the parent passed is the root node (so an invalid index) and to return 0 (no child) when the parent is valid (i.e. one of the connection...)

    Note that before I found that I added a columnCount() function so you may also need to add it so as to have your model working properly...
    Current Qt projects : QCodeEdit, RotiDeCode

  10. The following user says thank you to fullmetalcoder for this useful post:

    thomaspu (29th July 2007)

  11. #10
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Ah, yes, that was it! Yeah, you return the number of connections when the parent is invalid. Which in my case is always cause my list is flat and doesn't have a parent.

    Thanks for all the help, I think I'm good to go for a bit!
    Paul

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Coin3d + Qt: SIGLNALs and SLOTs
    By vonCZ in forum Newbie
    Replies: 26
    Last Post: 15th May 2009, 07:34
  3. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30
  4. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 13:54
  5. TreeView custom model
    By steg90 in forum Newbie
    Replies: 1
    Last Post: 9th May 2007, 10:06

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
  •  
Qt is a trademark of The Qt Company.