Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2007
Text alignment in QTableView 4.3


Message 1 in thread

Hi, has anyone figured out how to align text in a QTableView. ie (left 
center right).

Thanks

--
 [ signature omitted ] 

Message 2 in thread

On Tuesday 10 April 2007 23:28:27 Mark Stevens wrote:
> Hi, has anyone figured out how to align text in a QTableView. ie (left
> center right).
>
> Thanks
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

Hi,

In 4.2 it is as simple as having your data() function from your model to 
return a Qt::Alignment flag for the appropriate row/column. I haven't tried 
4.3 (yet).

Regards,
Stefan Vunckx

--
 [ signature omitted ] 

Message 3 in thread

On Wednesday 11 April 2007 00:00:54 Stefan Vunckx wrote:
> On Tuesday 10 April 2007 23:28:27 Mark Stevens wrote:
> > Hi, has anyone figured out how to align text in a QTableView. ie (left
> > center right).
> >
> > Thanks
> >
> > --
> > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> > "unsubscribe" in the subject or the body. List archive and information:
> > http://lists.trolltech.com/qt-interest/
>
> Hi,
>
> In 4.2 it is as simple as having your data() function from your model to
> return a Qt::Alignment flag for the appropriate row/column. I haven't tried
> 4.3 (yet).

I apologise,
And of course when role == Qt::TextAlignmentRole... I was a little too fast.
>
> Regards,
> Stefan Vunckx



--
 [ signature omitted ] 

Message 4 in thread

Stefan Vunckx wrote:
>> Hi,
>>
>> In 4.2 it is as simple as having your data() function from your model to
>> return a Qt::Alignment flag for the appropriate row/column. I haven't tried
>> 4.3 (yet).
> 
> I apologise,
> And of course when role == Qt::TextAlignmentRole... I was a little too fast.
>> Regards,
>> Stefan Vunckx
> 
> 
Hi Stefan, I didn't get it at first but finally got the solution. I was 
interested in a particular column so used the following.

ui.ipTableView->setItemDelegateForColumn ( 2, new column2Delegate(this) );


#include <QItemDelegate>

class column2Delegate : public QItemDelegate
{
     Q_OBJECT

public:

column2Delegate::column2Delegate(QObject *parent)
     : QItemDelegate(parent)
{
}

void column2Delegate::paint(QPainter *painter,
                           const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
	QDate text = index.model()->data(index, Qt::DisplayRole).toDate();
     QStyleOptionViewItem myOption = option;
     myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;

	drawDisplay(painter, myOption, myOption.rect, text.toString("MM-dd-yyyy"));
     drawFocus(painter, myOption, myOption.rect);
}

};


Thanks for the help.

Mark

--
 [ signature omitted ] 

Message 5 in thread

On Wednesday 11 April 2007 18:58:03 Mark Stevens wrote:
> Hi Stefan, I didn't get it at first but finally got the solution. I was
> interested in a particular column so used the following.
>
> ui.ipTableView->setItemDelegateForColumn ( 2, new column2Delegate(this) );
>
>
> #include <QItemDelegate>
>
> class column2Delegate : public QItemDelegate
> {
>      Q_OBJECT
>
> public:
>
> column2Delegate::column2Delegate(QObject *parent)
>
>      : QItemDelegate(parent)
>
> {
> }
>
> void column2Delegate::paint(QPainter *painter,
>                            const QStyleOptionViewItem &option,
>                            const QModelIndex &index) const
> {
> 	QDate text = index.model()->data(index, Qt::DisplayRole).toDate();
>      QStyleOptionViewItem myOption = option;
>      myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
>
> 	drawDisplay(painter, myOption, myOption.rect,
> text.toString("MM-dd-yyyy")); drawFocus(painter, myOption, myOption.rect);
> }
>
> };
>
>
> Thanks for the help.
>
> Mark
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

Yes,
but my point was that if you have a custom model, you *should* have a data() 
function (the one u call in ur code to get the date) inherited.

In THAT data function u can do:

QVariant
YourCustomModel::data( const QModelIndex &index, int role ) const
{
    /* more code */

    if ( index.column() == 2 && role == Qt::TextAlignmentRole ) {
        return Qt::AlignRight | Qt::AlignVCenter;
    }

    /* more code */
}

And then you dont need a QItemDelegate subclass of your own ;-)
Although I haven't tested if it works for QDate displays, but i'm assuming it 
does.

Regards,
Stefan Vunckx

--
 [ signature omitted ]