Results 1 to 13 of 13

Thread: Custom Model Class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Custom Model Class

    Hehe, I know that it won't be able to magically know what wants deleting, but what I am trying to do is have a QTableView with two buttons beneath it, Add and Remove. These should add rows to my derived model class. So what slot should these buttons be connected to? Slots of the model, or of the table view? And I assume I have to implement the slot myself? I can't seem to find any slots in QTableView which automatically do this anyway.

    I'm getting there with implementing the model, I'm now deriving from QAbstractItemModel where each item is a "Player" which contains a QString for name and a QString for email, etc.

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

    Default Re: Custom Model Class

    Quote Originally Posted by mattjgalloway View Post
    So what slot should these buttons be connected to? Slots of the model, or of the table view?
    Custom slot of the widget which holds the model or the view variable.

    And I assume I have to implement the slot myself?
    You assume correct. Any by this assumptions you should have already answered your previous question - if we're talking about a custom slot, it has to be in a class you're just creating...

    I can't seem to find any slots in QTableView which automatically do this anyway.
    The button doesn't emit any parameters when clicked, so how would you want the view that only displays the model to know what you want to add to the model?

  3. #3
    Join Date
    Jun 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Custom Model Class

    Well exactly... if the button doesn't emit any parameters, that's why I was confused as to how to implement the adding.

    So... I think I've worked it out... in that I have to add a slot into the mainWindow class (which holds the model, view and button) and have a slot there for addPlayer, removePlayer which looks at the current index of the view and then adds to the model at the correct place. I'm guessing that looks reasonable?

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

    Default Re: Custom Model Class

    Yes, it's fine.

  5. #5
    Join Date
    Jun 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Custom Model Class

    Argh I'm now coming across another problem...

    My QTableView which I am using to view the model can't see to edit the model! I've implemented a flags() function which is like so:
    Qt Code:
    1. if (!index.isValid())
    2. return Qt::ItemIsEnabled;
    3.  
    4. return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    To copy to clipboard, switch view to plain text mode 

    So that should return that it's editable, selectable, etc. But when I try to click on a cell, nothing happens, it doesn't turn blue or allow me to edit, nothing!

    Any ideas?

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

    Default Re: Custom Model Class

    Did you implement the model to return some data when asked for EditRole? Did you set the edit triggers for the view that fit your needs?

  7. #7
    Join Date
    Jun 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Custom Model Class

    I think I figured out why it wasn't working - can't remember what it was now, but it seems to be fine.

    Now I have yet another problem... I have switched to using a QTreeView because that seems actually to be more what I want. I don't actually have a tree structure, but the view looks better than a QTableView for my functionality.

    But my custom model I have created doesn't work properly. Basically whenever I add an item, it gets put into the list, but also as a child of itself, and thus a child of that, and so on and so on. Adding another new entry makes that a child of the first, and etc, etc. It's really odd.

    I'm fairly sure this is something to do with the way my index() and parent() functions are defined. Currently they are thus:

    Qt Code:
    1. QModelIndex PlayersModel::index(int row, int column, const QModelIndex &parent) const
    2. {
    3. return createIndex(row, column);
    4. }
    5.  
    6. QModelIndex PlayersModel::parent(const QModelIndex &index) const
    7. {
    8. return QModelIndex();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas what I am doing wrong?

  8. #8
    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: Custom Model Class

    The index() function shouldn't return a "blank" index like this... Index created by it are used by the view to iterate over your data and display it properly. The great big thing here is to clearly define how you associate model indexes with your underlying data. QModelIndex provides a way of associating either a void* or an integer which can be retrieved later on (mainly in functions like
    data(), flags(), setData(), ... but not only). What's wrong with your code is that you discard the parent passed to createIndex()...

    If this parent is not valid it means that the (row, col) are relative to the root of the tree. Otherwise they are children of the parent index passed. and QTreeView keeps adding children. To fix this just use this code (I'mm assuming that you really don't use any tree structure here...)
    Qt Code:
    1. QModelIndex PlayersModel::index(int row, int column, const QModelIndex& parent)
    2. {
    3. if ( parent.isValid() )
    4. return QModelIdex();
    5. return createIndex(row, column);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #9
    Join Date
    Jun 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Custom Model Class

    Thanks! I had literally just figured it out as well, but went with:

    Qt Code:
    1. if(parent == QModelIndex())
    2. return createIndex(row, column);
    3. else
    4. return QModelIndex();
    To copy to clipboard, switch view to plain text mode 

    Is your code more valid? It looks like it'll do pretty much the same thing. I'm just trying to see if the code I came up with has any obvious design flaw you see, as I'm trying to learn good programming technique at the same time as coding.

    Many thanks!

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. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 13:54
  4. TreeView custom model
    By steg90 in forum Newbie
    Replies: 1
    Last Post: 9th May 2007, 10:06
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.