PDA

View Full Version : Vertical alignment of rows in a tree view



Micawber
17th June 2008, 16:32
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 (http://www.qtcentre.org/forum/f-qt-programming-2/t-setting-row-height-in-qtreeview-3156.html) thread! :D)

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

jpujolf
17th June 2008, 16:47
In the "data" method add a piece of code like this :



QVariant MyTreeModel::data ( const QModelIndex & Index, int Role ) const
{
...
if ( Role == Qt::TextAlignmentRole )
return Qt::AlignVCenter | Qt::AlignHCenter;
...
}


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

Micawber
17th June 2008, 16:57
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

jpujolf
17th June 2008, 17:18
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 (http://doc.trolltech.com/4.4/qtreeview.html#uniformRowHeights-prop) seems to point in that direction...

jpn
17th June 2008, 17:21
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 (http://www.qtcentre.org/forum/f-qt-programming-2/t-qtreeview-alignment-14193.html). Please search the forums next time.

Micawber
17th June 2008, 20:27
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 (http://doc.trolltech.com/4.4/qtreeview.html#uniformRowHeights-prop) seems to point in that direction...

I tried it, but no joy.

jpn
17th June 2008, 20:31
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.

Micawber
17th June 2008, 20:39
This topic was discussed two days ago (http://www.qtcentre.org/forum/f-qt-programming-2/t-qtreeview-alignment-14193.html). 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...


void TreeViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
QStyleOptionViewItem styleOption = option;
styleOption.displayAlignment = Qt::AlignCenter;
QItemDelegate::paint( painter, styleOption, index );
}


(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

jpn
17th June 2008, 21:25
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:


QVariant QDirModel::data(const QModelIndex &index, int role) const
{
...
if (index.column() == 1 && Qt::TextAlignmentRole == role) {
return Qt::AlignRight;
}
...
}

This overrides what you pass in the style option. The workaround is to reimplement QDirModel::data()...

Micawber
17th June 2008, 22:06
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:


QVariant QDirModel::data(const QModelIndex &index, int role) const
{
...
if (index.column() == 1 && Qt::TextAlignmentRole == role) {
return Qt::AlignRight;
}
...
}

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