PDA

View Full Version : Confusion over columnCount() in itemviews



edice
24th September 2009, 02:07
Hi,

Is it me, or is the columnCount() design poorly thought out?

The method is:
int QAbstractItemModel::columnCount( QModelIndex parent )

and yet, if you try to return a different # of columns depending on the parent, things don't work.

eg, I want a QTreeView with the root items to have one column (and spanned), and then the child items to all have 2 columns.

However, if you do this:
int Model::columnCount( QModelIndex parent )
{
if (parent.isValid())
return 2;
return 1;
}

And add the model to a treeview, then it will always only show 1 column.

So, apparently, trees are supposed to have a fixed number of columns.
Tables are obviously supposed to have a fixed number of columns.
and Lists usually have just 1 column.


So why pass the parent? its pointless and confusing, instead it should be
int Model::columnCount()

or am I missing something?

Paul

caduel
24th September 2009, 07:40
the question is whether the view you are using supports what a model "offers". e.g. a QTableView or a QListView ignore children, a QListView has only 1 column, a QTreeView has as many colums as the top-level indexes have etc.
Still you may use them with models that have these things...

edice
24th September 2009, 09:50
The point I'm trying to make is that *none* of the views fully support variable numbers of columns. eg, even in the Tree view, you have to set the top level # of columns to the maximum number of columns ANY child have. And thats not explicitly documented!

I think I am using the wrong tool for the job. I want to display and edit configuration settings... I want to be able to add and remove items, and "inside" those items there is a set of properties that can be modified. The properties depends on the item added.

If I were making ... uuh ... an addressbook, it would be something like this...

Person
- Name
- Address
- house number
- postcode
- Hair colour
- mobile phone

Pet
- Name
- Family name
- address (number, address, postcode - maybe all on one line)
- dog collar colour

etc. Add another Person, and another Person dataset appears below the 'Pet' dataset...

This would be easy to do on a webpage... how can this be done in QT ?

cheers
Paul

edice
24th September 2009, 10:15
Actually that is a pretty crap example, but the point remains,

Users can add multiple Items (like Person), and under that, there are a bunch of settings. In each Setting, there might be another set of Settings, which is why I originally chose to go with a Tree.

The problem is that it just doesn't work well...

a) If you put the Setting name in the first column, and the value (to edit) in the second column, you can't seem to cursor up and down, or just start typing (like excel) to edit the value.

b) The first column resizes to fit the collapsed columns, so when you expand to see the settings, the text is clipped

c) It is painful to program the model. I've done it, but I have to use the internalId to keep track of which row is for what type of setting.


I'm thinking instead, maybe just have a QScrollArea and a QVBoxLayout. Then each time the user adds an Item, I add a widget that is coded with all the configuration items inside. User edits that widget and I save the changes.

Kind of like this forum... you have entries, and for each entry you can edit, reply, check out the user info, read the text, etc. Each entry could have a different set of actions (eg an attached an image means an additional action to view the image).


So I guess I'm trying to simulate a webpage, but with layouts and widgets... does anyone else do this?

What do other people do when they need to configure a variable number of items?

caduel
24th September 2009, 13:17
The problem is that a QTreeView has "columns" that are the same over all the model's hierarchy. This is not what you want.

A common workaround (that is applicable when you have a common structure with a header each; e.g.
Person A "Name Surname ...."
- Adress
- email
- phone
Person B "Name Surname ...."
- Adress
- email
- phone

You can just use the column structure of the subrecords and use QTreeView::setFirstColumnSpanned() for the header items.
This has lots of drawbacks but is the easiest thing if you don't want to write your own delegates, views or so. For simple 2-level hierarchies it often is enough.

edice
24th September 2009, 15:23
The problem is that a QTreeView has "columns" that are the same over all the model's hierarchy. This is not what you want.

A common workaround (that is applicable when you have a common structure with a header each; e.g.
Person A "Name Surname ...."
- Adress
- email
- phone
Person B "Name Surname ...."
- Adress
- email
- phone

You can just use the column structure of the subrecords and use


This is what I have tried. The immediate problem with this is the column widths don't seem to expand for the first column correctly, and you can't just start typing to enter data, like you can with an Excel table... you have to double-click. If you press enter, it clicks Ok on my Dialog.

It also looks crap. I know you can do all sorts of things with style sheets, but I find the documentation and examples sparse and not complete enough. I normally don't mind diving into things and trying stuff out, but without the equivalent of Firebug for QT, I think it'll involve a lot of messing about to find the right look. It feels like driving a tank to the shops.



QTreeView::setFirstColumnSpanned() for the header items.


I have no idea how I could possibly use setFirstColumnSpanned(), because you need to specify the row AND the QModelIndex. How do I know which rows and which model indexes? And that is a setting in the View, not the Model, so ... do I create a slot, connect up all the dataChanged signals to my slot, then query my model for all the first level items and then call setFirstColumnSpanned() on those rows ? I just can't imagine how it is done with a dynamic model.



This has lots of drawbacks but is the easiest thing if you don't want to write your own delegates, views or so. For simple 2-level hierarchies it often is enough.

It seems like a huge hack to do anything remotely different to a directory tree. I am getting the impression that ItemViews is just not the right tool for this job (ie configuration editing and navigation).