[Qt-interest] Alignment in QTableView
Tibor Kiss
tibor at wiesen.at
Fri May 28 19:53:35 CEST 2010
I guess you could work out the solution yourself, but if it helps anyway
here it is:
class ToTheMiddleDelegate : public QItemDelegate
{
Q_OBJECT
public:
ToTheMiddleDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const
QModelIndex &index) const;
};
ToTheMiddleDelegate::ToTheMiddleDelegate (QObject *parent) : QItemDelegate
(parent)
{ }
void ToTheMiddleDelegate::paint(QPainter *painter, const
QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString text = index.model()->data(index, Qt::DisplayRole).toString();
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignHCenter | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, text);
drawFocus(painter, myOption, myOption.rect);
}
... and you use this delegate, with the
QTableView::setItemDelegateForColumn(delegate).
Yeah I agree, it's not so straightforward, it has it's advantages and
disadvantages. Now we live with that.
Tibor
2010/5/27 Yves Bailly <yves.bailly at sescoi.fr>
> Andre Somers a écrit :
> > On 26-5-2010 17:22, Yves Bailly wrote:
> >> Here's a (probably) basic question, but I really can't find a
> >> solution. Using a QTableView to display the contents of an
> >> SQL database (using a QSqlTableModel), I would just want to
> >> have a right-aligned column... but I just can't find a non-insane
> >> way of doing this SIMPLE thing...
> >>
> > Normally, alignment is controlled by one of the roles for the data
> > method of the model. You have three options:
> > 1) subclass QSqlTableModel and reimplement data to return the correct
> > value for the alignment role and the column you want.
> > 2) create a proxy model implementation that basically does the same
> > (which is a bit more flexible, as you could apply it to other existing
> > models such as the file system model).
> > 3) create a delegate that overrides the alignment set by the model.
> >
> > In my view, all of the above are valid approaches.
>
> I've read the thread...
>
> I came asking on the list because I had already found the approaches
> you mention here... I just find all of them insanely complex just to
> change alignment of a column, in a small prototype-like project. They
> may be sensible if it was a 100KLOC project, but it's not.
>
> So I guess I'll just do things "the good'old way", as Rob said, and
> stick with QTableWidget for now.
>
> Thanks all for your inputs.
>
> --
> /- Yves Bailly - Software developper -\
> \- Sescoi R&D - http://www.sescoi.fr -/
> "The possible is done. The impossible is being done. For miracles,
> thanks to allow a little delay."
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100528/06142a39/attachment.html
More information about the Qt-interest-old
mailing list