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?
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?
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:
{ return createIndex(row, column); } { }To copy to clipboard, switch view to plain text mode
Any ideas what I am doing wrong?
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:
{ if ( parent.isValid() ) return QModelIdex(); return createIndex(row, column); }To copy to clipboard, switch view to plain text mode
Current Qt projects : QCodeEdit, RotiDeCode
Thanks! I had literally just figured it out as well, but went with:
Qt Code:
return createIndex(row, column); elseTo 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!
Bookmarks