PDA

View Full Version : QGraphicsRectItem and QGraphicsTextItem.



cydside
19th July 2009, 22:43
Hi to all,
I'm trying to build a rectangle (QGraphicsRectItem) with a text inside (QGraphicsTextItem) perfectly centered both vetically and horizontally as child, if it's the best solution. I've tried the following code:



for (int i = 0; i < 12; i++)
{
QGraphicsRectItem *month =
new QGraphicsRectItem(QRect(margineX + 60 * i, endY - margineY + 2, 60, 30), 0, scene);
month->setPen(QPen(Qt::red, 1, Qt::SolidLine));
month->setBrush(Qt::NoBrush);

QGraphicsTextItem *textMonth =
new QGraphicsTextItem(QString("%1").arg(i + 1), month);
}


and...it doesn't work. Thanks in advance!

Lykurg
19th July 2009, 22:50
It's easier to subclass the paint method of the text item and draw a border yourself. Then call the base class paint method for the text drawing.

cydside
19th July 2009, 22:55
Thanks Lykurg. do you know where can I find an example? if it's not so much trouble :o

Lykurg
19th July 2009, 23:05
It's always better to try by yourself,but if you really want for that simple task:

class myItem : public QGraphicsTextItem
{
public:
myItem(QGraphicsItem *parent = 0) : QGraphicsTextItem(parent)
{}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
{
painter->setPen(Qt::black);
painter->setBrush(QColor(230,230,230));
painter->drawRect(option->rect);
QGraphicsTextItem::paint(painter, option, widget);
}
};

cydside
19th July 2009, 23:44
Thanks again Likurg, I've used your code but I've not centered the problem cause my incomplete explaination. So, I'd like to build histograms (QGraphicsRectItem) and write the value inside the rectangle (see the attachment) and I'm going on trouble to find something that works. Any idea?

nish
20th July 2009, 01:50
just use qpainter transforms to write vertical text

wagmare
20th July 2009, 05:33
create a custom QGraphicsItem with both QGraphicsRect and QGraphicsText
and in QGraphicsView class (parent)
QTransform transform;
transform.rotate( 90);
grpahicsItem->setTransform(transform);

cydside
20th July 2009, 06:07
create a custom QGraphicsItem with both QGraphicsRect and QGraphicsText
and in QGraphicsView class (parent)
QTransform transform;
transform.rotate( 90);
grpahicsItem->setTransform(transform);

Ok, It seems a good solution, but how to obtain a text centered both vetically and horizontally in the rectangle?

wagmare
20th July 2009, 06:19
but how to obtain a text centered both vetically and horizontally in the rectangle?

thats what i am suggesting .. combine both the text and rectangle into single QGraphicsItem ..

paint such that
create a custom QGraphicsItem()

class CustomItem : public QGraphicsRectItem
{
Q_OBJECT

public:
CustomItem(const QRectF &rect);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *);
QRectF boundingRect();

};



CustomItem::CustomItem(const QRectF &rect)
: QGraphicsRectItem(rect)
{
textItem->setDefaultTextColor(QColor(255,255,255,255));
textItem->setPos(x, y); //here u set position of text
textItem->setPlainText(message); //set your text here
}

void CustomItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *widget)
{
painter->setBrush(QBrush(QColor(21, 155, 210, 255)));
painter->drawRect(rect());

}

now the item is ready in horizontal ... now in parent graphicsView() rotate it using previous code ...

Lykurg
20th July 2009, 08:48
create a custom QGraphicsItem with both QGraphicsRect and QGraphicsText
and in QGraphicsView class (parent)
QTransform transform;
transform.rotate( 90);
grpahicsItem->setTransform(transform);

Instead of setting the transformation to each item you use and in your particular case, it's better to create your own item and draw the text yourself:
In paint also draw the text via QPainter::drawText() (there are alignment flags) and then rotate the painter by 90°. And don't forget to implement the sizeHint() function.

wagmare
20th July 2009, 09:05
yes lykurg .. its a better option .. in paint() itself we can rotate the text and add the text in that custom item ...

cydside
20th July 2009, 11:32
Instead of setting the transformation to each item you use and in your particular case, it's better to create your own item and draw the text yourself:
In paint also draw the text via QPainter::drawText() (there are alignment flags) and then rotate the painter by 90°. And don't forget to implement the sizeHint() function.

QPainter will be the best solution when the rect area is smallest than the text, but if I try to create a QPainter object for that QGraphicsScene as the following code:


QGraphicsScene *scene =
new QGraphicsScene(QRect(0, 0, endX, endY), this);

for (int i = 0; i < 12; i++)
{
QGraphicsRectItem *mese =
new QGraphicsRectItem(QRect(margineX + 2 + 60 * i, endY - margineY + 2, 60, 30), 0, scene);
// mese->setPen(QPen(Qt::red, 1, Qt::SolidLine));
mese->setPen(Qt::NoPen);
mese->setBrush(Qt::NoBrush);


QPainter painter(scene);
painter.drawText(mese, Qt::AlignCenter, mesiAnno.at(i));
painter.drawRect(mese);


the compiler is not agree with me because:



error: no matching function for call to `QPainter::QPainter(QGraphicsScene*&)'
note: candidates are: QPainter::QPainter(const QPainter&)
note: QPainter::QPainter(QPaintDevice*)


where do i have to link my QPainter object??? :crying:

thanks guys, I really appreciate your comments!

wagmare
20th July 2009, 12:08
where u are using the painter ...
if in QGraphicsView () class
then paint inside

void QGraphicsView::drawBackground

if in QGraphicsItem() class
then paint inside

void QGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )

Lykurg
20th July 2009, 12:11
That is what I have meant by, do stuff (subclassing) yourself and try to understand!

Use my first example code and do the painting inside the paint function. You don't want to paint on the screen, you want to the item, and that is done inside the item!