Results 1 to 10 of 10

Thread: Vertical alignment of rows in a tree view

  1. #1
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Vertical alignment of rows in a tree view

    I'm using a QDirModel with a QTreeView. I have set up a delegate that changes the default sizehint for the row height so that I can have 30 pixel rows for my fat fingers using the touch screen. (Thanks to wysota from this thread! )

    The problem now is that while most of the data across the row lines up in the center of the space, one column (file size) insists on being aligned with the top and it makes for a rather confusing display.

    Is there a way to force the vertical alignment of the rows in the tree view?

    Any help is greatly appreciated.

    -Mic

  2. #2
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Vertical alignment of rows in a tree view

    In the "data" method add a piece of code like this :

    Qt Code:
    1. QVariant MyTreeModel::data ( const QModelIndex & Index, int Role ) const
    2. {
    3. ...
    4. if ( Role == Qt::TextAlignmentRole )
    5. return Qt::AlignVCenter | Qt::AlignHCenter;
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    And your data will appear horizontally / vertically centered...

  3. #3
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Vertical alignment of rows in a tree view

    Thanks for the input. I'm really trying to not have to sub-class QDirModel (I don't even know if its possible). If I can't figure another way, I may have to resort to using your solution.

    -Mic

  4. #4
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Vertical alignment of rows in a tree view

    Quote Originally Posted by Micawber View Post
    Thanks for the input. I'm really trying to not have to sub-class QDirModel (I don't even know if its possible). If I can't figure another way, I may have to resort to using your solution.

    -Mic
    Oh, OK. Have you tried this ?

    model->setData ( model->index ( 0, 0 ), Qt::AlignVCenter, Qt::TextAlignmentRole );
    treeview->setUniformRowHeights ( true );

    It may run. In fact I never tested it, but documentation here seems to point in that direction...

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Vertical alignment of rows in a tree view

    Quote Originally Posted by Micawber View Post
    I'm really trying to not have to sub-class QDirModel (I don't even know if its possible).
    Of course it's possible.

    If I can't figure another way, I may have to resort to using your solution.
    This topic was discussed two days ago. Please search the forums next time.
    J-P Nurmi

  6. #6
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Vertical alignment of rows in a tree view

    Quote Originally Posted by jpujolf View Post
    Oh, OK. Have you tried this ?

    model->setData ( model->index ( 0, 0 ), Qt::AlignVCenter, Qt::TextAlignmentRole );
    treeview->setUniformRowHeights ( true );

    It may run. In fact I never tested it, but documentation here seems to point in that direction...
    I tried it, but no joy.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Vertical alignment of rows in a tree view

    QDirModel is unable to store Qt::TextAlignmentRole by design. To do so, you would have to subclass and reimplement data() (and possibly setData()) to handle that role.
    J-P Nurmi

  8. #8
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Vertical alignment of rows in a tree view

    Quote Originally Posted by jpn View Post
    This topic was discussed two days ago. Please search the forums next time.
    Thanks jpn. I did search for this but evidently I need better search terms. I'll try harder next time.

    Anyway, I implemented the QAbstractItemDelegate::paint() method in my delegate with the following code...

    Qt Code:
    1. void TreeViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. QStyleOptionViewItem styleOption = option;
    4. styleOption.displayAlignment = Qt::AlignCenter;
    5. QItemDelegate::paint( painter, styleOption, index );
    6. }
    To copy to clipboard, switch view to plain text mode 

    (TreeViewDelegate is a subclass of QItemDelegate if that makes any difference)

    but it didn't work. Am I doing anything wrong that you can see?

    -Mic

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Vertical alignment of rows in a tree view

    Aha, now I noticed the problem. I must admit didn't read the first post that carefully, I just thought that it was about general alignment, not just particular problematic column. There seems to be a problematic block of code in QDirModel:
    Qt Code:
    1. QVariant QDirModel::data(const QModelIndex &index, int role) const
    2. {
    3. ...
    4. if (index.column() == 1 && Qt::TextAlignmentRole == role) {
    5. return Qt::AlignRight;
    6. }
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    This overrides what you pass in the style option. The workaround is to reimplement QDirModel::data()...
    J-P Nurmi

  10. #10
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Vertical alignment of rows in a tree view

    Quote Originally Posted by jpn View Post
    Aha, now I noticed the problem. I must admit didn't read the first post that carefully, I just thought that it was about general alignment, not just particular problematic column. There seems to be a problematic block of code in QDirModel:
    Qt Code:
    1. QVariant QDirModel::data(const QModelIndex &index, int role) const
    2. {
    3. ...
    4. if (index.column() == 1 && Qt::TextAlignmentRole == role) {
    5. return Qt::AlignRight;
    6. }
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    This overrides what you pass in the style option. The workaround is to reimplement QDirModel::data()...
    Thanks jpn. I have reimplemented QDirModel::data() as you suggested and returned the correct alignment parameters and it works.

    I plan to file this as a bug.

    -Mic

Similar Threads

  1. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50

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.