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

Qt-interest Archive, March 2002
Re: Text alignment in QTable


Message 1 in thread

Am 01.12.01 21:19:59, schrieb Steven Nakhla <sdnakhla@yahoo.com>:

>Is there a way I can align the text in a QTable item
>to the right?  Anybody know?
>
>
>Steve Nakhla
>
>__________________________________________________
>Do You Yahoo!?
>Buy the perfect holiday gifts at Yahoo! Shopping.
>http://shopping.yahoo.com
>
>--
>List archive and information: http://qt-interest.trolltech.com
>
>
>
>

Hi!

I was looking around, because I had the same problem with the auto-align of QTable items
and I saw that nobody had given a short answer.

This is the way I solved the problem:
<music playtatus="play">
... I do it my way ...
<\music>

// definition of new classes:
class DQLeftAlignTableItem : public QTableItem
{
public:
	DQLeftAlignTableItem( QTable *table, EditType et, const QString &text ) 
		: QTableItem(table, et, text) 
	{}
	DQLeftAlignTableItem( QTable *table, EditType et, const QString &text, const QPixmap &p ) 
		: QTableItem(table, et, text, p) 
	{}

	int alignment() const {return Qt::AlignLeft | Qt::AlignVCenter;	} // Here is the align change done.
};

class DQRightAlignTableItem : public QTableItem
{
public:
	DQRightAlignTableItem( QTable *table, EditType et, const QString &text ) 
		: QTableItem(table, et, text) 
	{}
	DQRightAlignTableItem ( QTable *table, EditType et, const QString &text, const QPixmap &p ) 
		: QTableItem(table, et, text, p) 
	{}

	int alignment() const {return Qt::AlignRight | Qt::AlignVCenter;	} // Here is the align change done.
};

//... some code ...
QTableItem *pItem = NULL;

pItem = new DQRightAlignTableItem(m_table, QTableItem::Never, "This Text is shown right-align.");
if(NULL==pItem)
	qDebug(tr("NULL==DQRightAlignTableItem "));
else
	m_table->setItem(j, i, pItem);

//... some code ...

pItem = new DQLeftAlignTableItem (m_table, QTableItem::Never, "4711");  // This number is show left-align.
if(NULL==pItem)
	qDebug(tr("NULL==DQLeftAlignTableItem "));
else
	m_table->setItem(j, i, pItem);

//... some code ...

I think it's the shortest solution possible, or not?

Erik Hartmann
--
 [ signature omitted ] 

Message 2 in thread

> >Is there a way I can align the text in a QTable item
> >to the right?  Anybody know?
> >
> >
> Hi!
> 
> I was looking around, because I had the same problem with the 
> auto-align of QTable items
> and I saw that nobody had given a short answer.

[snipped some code]

> I think it's the shortest solution possible, or not?

Not the shortest... ;-)

It seems a bit overkill to make two subclasses, one right aligned
and one left aligned. A more reusabel methode would be to make
one QTableItem subclass and introduce a setAlignment(int aFlags) function.

-- 
 [ signature omitted ]