[Qt-interest] applying Tree model to Table view or convert Tree model to Table model

Stephen Kelly steveire at gmail.com
Wed Jun 23 17:40:57 CEST 2010


Valeriy Portnyagin wrote:

> 
>  2. create one data model like Tree(from XML file) contains all data.
> like this:
>         "1. chapter One
>                 plain text
>                 special text 1
>                         property 11
>                         property 12
>                         property 13
>                 plain text
>                 1.2. chapter oneTwo
>                         special text 2
>                                 property 21
>                                 property 22
>                                 property 23
>                         plain text
>         2. chapter Two
>                 special text 3
>                         property 31
>                         property 32
>                         property 33
>         .............."

You should consider accessing the special properties though custom roles.

class MyModel : public QStandardItemModel 
{
  Roles {
    CustomPropertiesRole = Qt::UserRole
  };
};

class MyStandardItem : public QStandardItem
{
  void setCustomProperties(const QVariantHash &mapping)
  { 
    m_customProperties = mapping;
  }

  QVariant data(int role)
  {
    if (role != CustomPropertiesRole)
      return QStandardItem::data(role);

    return m_mapping;
  }
}

Or something like that. I haven't used QStandardItemModel much.

>  
>         3.2. for custom Table displayed all data in plain structure(each
> item in own row), but for special text properties add needed count of
> column for display them. like following:
> 
> 
> special text property 1
> special text property 2
> special text property 3
> 1. chapter One
> 
> 
> 
> plain text
> 
> 
> 
> special text 1
> property 11
> property 12
> property 13
> plain text
> 
> 
> 
> 1.2. chapter oneTwo
> 
> 
> 
> special text 2
> property 21
> property 22
> property 23
> plain text
> 
> 
> 
> 2. chapter Two
> 
> 
> 
> special text 3
> property 31
> property 32
> property 33
> 
> It was an introduction. :)

Was this supposed to look like tabular data? I got it as plain text. You'll 
need to make a plain text version of the table it it should be tabular.

http://lists.trolltech.com/pipermail/qt-interest/2010-June/024705.html

> 
> point 1 and 2 are done but in the point 3  i have some issues. My aim is
> to have one Tree Model which is partly applied for Tree View and special
> applied or converted for Table View. I am trying to solve these tasks
> using QStandardItemModel, QSortFilterProxyModel with QTreeView,
> QTableView.
> 
> 1. I can't apply Tree model for Table view with right displaying all
> items. Only the root items are displayed in the Table view without their
> children. Items "chapter 1, chapter 2" will be visible in my example. If i
> need to see all items i have to change the parents for all items, but it
> is impossible(i didn't see such method in the class). How can i see all
> items in the Table view  if i use Tree model?
> 
> 2. It is impossible to create such Table view as i drew with several
> additional columns from my general Tree model. I  can do it using
> QTableWidget but i am losing flexibility of model/view programming. My
> QTableWidget will be independent from my model. Is there a way to avoid
> it?
> 
> 3. For displaying the part of general Tree model i have the class
> QSortFilterProxyModel. But i have to create huge and difficult regular
> expression for filtering several items. In my case i will create regular
> expression for filtering all special text properties. If i have 10
> properties my regexp will be huge or difficult  or impossible. am i right
> or it can be done more elegant?

You can reimplement QSortFilterProxyModel::filterAcceptsRow to do custom 
filtering. It's a virtual method.

All the best,

Steve.

> 
> I will appreciate any advices.
> 
> regards,
> Valery Portnyagin.





More information about the Qt-interest-old mailing list