Results 1 to 2 of 2

Thread: Model/Delegate/Proxy

  1. #1

    Default Model/Delegate/Proxy

    Hello together I'm experimenting with qt and have some questions about this whole
    Model-Delegate-Proxy stuff.

    What I want to achieve is to show a list of Persons in a QListView, with the ability of
    filtering, and showing letter-captions, (Like many List-Controls on mobileOS) and writing the whole information for a person in 1 cell.

    -----------------------
    |A |
    -----------------------
    |Abigail Familly-Name |
    |Audrey Familly-Name |
    -----------------------
    |C |
    -----------------------
    |Christian Familly-Name|
    -----------------------
    |D |
    -----------------------
    |Daniel Familly-Name |
    -----------------------

    I have a Class: Person where I have the structure of the information (Name,Famillyname,day of b.... aso.)

    On the MODEL side I have a QAbstractListModel
    Qt Code:
    1. ..........
    2. explicit PersonModel(QObject *parent = 0);
    3.  
    4. int columnCount(const QModelIndex &parent) const;
    5. int rowCount( const QModelIndex &index = QModelIndex()) const;
    6.  
    7. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    8. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    9. void FillData();
    10.  
    11. private:
    12. Q_DISABLE_COPY(PersonModel);
    13. QList<Person*> Persons;
    14. ...............
    To copy to clipboard, switch view to plain text mode 
    My data() function looks like this:
    Qt Code:
    1. QVariant PersonModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (role == Qt::DisplayRole)
    4. {
    5. qRegisterMetaType<Person>("Person");
    6. v.setValue<CPerson>(*Persons.at(index.row()));
    7. return v;
    8. }
    9. return QVariant();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Since I want to write different parts of the person class in on cell I return the
    whole structure in a QVariant.

    On the DELEGATE side my paint looks like this:
    Qt Code:
    1. void PersonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if (index.column() == 0) {
    4. Person entry =index.model()->data(index).value<Person>();
    5. painter->setFont( QFont("Arial",14,QFont::Bold));
    6. QRect NameRect = option.rect;
    7. NameRect.setLeft(5);
    8. painter->drawText(NameRect,Qt::AlignLeft,entry.GetName());
    9. NameRect.setLeft(0);
    10. NameRect.setRight(NameRect.width()-5);
    11. painter->setFont( QFont("Arial",14,QFont::Cursive));
    12. painter->drawText(NameRect,Qt::AlignRight,entry.GetSurename());
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Here I'm placing Name and Surename.

    On the proxy side I have QSortFilterProxyModel.
    Qt Code:
    1. bool MySortFilterProxyModel::lessThan(const QModelIndex &left,const QModelIndex &right) const
    2. {
    3. Person leftData = sourceModel()->data(left).value<Person>();
    4. Person rightData = sourceModel()->data(right).value<Person>();
    5.  
    6. QString leftString = leftData.GetName();
    7. QString rightString = rightData.GetName();
    8.  
    9. return QString::localeAwareCompare(leftString, rightString) < 0;
    10. }
    11.  
    12. bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
    13. const QModelIndex &sourceParent) const
    14. {
    15. QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    16. Person data = sourceModel()->data(index0).value<Person>();
    17. QString strData = data.GetName();
    18. return (strData.contains(filterRegExp()));
    19. }
    To copy to clipboard, switch view to plain text mode 
    This stuff works so far.

    Now I have 2 problems.
    1. If I set the proxy filter e.g. on 'Da' the proxy is also filtering-out the Letter-Caption 'D', so that the List looks like
    -----------------------
    |Daniel Familly-Name |
    -----------------------
    What I have tried is to add a additional flag in my person class (Letter Caption yes/ no) and than to check this flag in filterAcceptsRow(). But than the list looks like this

    -----------------------
    |A |
    -----------------------
    -----------------------
    |C |
    -----------------------
    -----------------------
    |D |
    -----------------------
    |Daniel Familly-Name |
    -----------------------
    The letter-captions should only be drawn when there is a name.

    My 2. problem is:
    How can I convince Paint() and sizeHint() to draw this letter-captions in a
    different way. (height of cell,BG-Color)?

    Anyhow, I think that UserRoles could be the solution for my problems,
    but my experiments with UserRoles were not really successful.

    Have you any ideas how I could implement something like this, or is there
    a better way of doing this, than the way I tried?

    Thanks!

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

    Default Re: Model/Delegate/Proxy

    There are at least two possible approaches. One is to determine in the delegate and in the proxy whether the index contains the address card or the prefix header and act accordingly in paint() and in filterAcceptsRow(). Another is to make your model a tree model where prefixes are at level 0 of the model and address cards at level 1 and only filter out items at level 1 (adjusting filterAcceptsRow accordingly) and painting items differently in paint() depending on their level in the hierarchy. You could either use QTreeView with a customized tree drawing to get rid of branches and indentation or a QListView with a proxy model that flattens the original model (but painting and filtering should still be aware of the underlying model).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Custom Model and Proxy Model
    By frank100 in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2010, 14:30
  2. Replies: 7
    Last Post: 3rd November 2010, 23:18
  3. A Totalling Proxy Model?
    By ChrisW67 in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2010, 03:06
  4. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  5. Model and Proxy
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2006, 21:05

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.