QGraphicsRectItem and QGraphicsTextItem.
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:
Code:
for (int i = 0; i < 12; i++)
{
month
->setPen
(QPen(Qt
::red,
1, Qt
::SolidLine));
month->setBrush(Qt::NoBrush);
}
and...it doesn't work. Thanks in advance!
Re: QGraphicsRectItem and QGraphicsTextItem.
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.
Re: QGraphicsRectItem and QGraphicsTextItem.
Thanks Lykurg. do you know where can I find an example? if it's not so much trouble :o
Re: QGraphicsRectItem and QGraphicsTextItem.
It's always better to try by yourself,but if you really want for that simple task:
Code:
{
public:
{}
{
painter->setPen(Qt::black);
painter
->setBrush
(QColor(230,
230,
230));
painter->drawRect(option->rect);
}
};
1 Attachment(s)
Re: QGraphicsRectItem and QGraphicsTextItem.
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?
Re: QGraphicsRectItem and QGraphicsTextItem.
just use qpainter transforms to write vertical text
Re: QGraphicsRectItem and QGraphicsTextItem.
create a custom QGraphicsItem with both QGraphicsRect and QGraphicsText
and in QGraphicsView class (parent)
QTransform transform;
transform.rotate( 90);
grpahicsItem->setTransform(transform);
Re: QGraphicsRectItem and QGraphicsTextItem.
Quote:
Originally Posted by
wagmare
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?
Re: QGraphicsRectItem and QGraphicsTextItem.
Quote:
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()
Code:
{
Q_OBJECT
public:
CustomItem
(const QRectF &rect
);
};
Code:
CustomItem
::CustomItem(const QRectF &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
}
{
painter->drawRect(rect());
}
now the item is ready in horizontal ... now in parent graphicsView() rotate it using previous code ...
Re: QGraphicsRectItem and QGraphicsTextItem.
Quote:
Originally Posted by
wagmare
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.
Re: QGraphicsRectItem and QGraphicsTextItem.
yes lykurg .. its a better option .. in paint() itself we can rotate the text and add the text in that custom item ...
Re: QGraphicsRectItem and QGraphicsTextItem.
Quote:
Originally Posted by
Lykurg
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:
Code:
for (int i = 0; i < 12; i++)
{
// mese->setPen(QPen(Qt::red, 1, Qt::SolidLine));
mese->setPen(Qt::NoPen);
mese->setBrush(Qt::NoBrush);
painter.drawText(mese, Qt::AlignCenter, mesiAnno.at(i));
painter.drawRect(mese);
the compiler is not agree with me because:
Code:
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!
Re: QGraphicsRectItem and QGraphicsTextItem.
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 )
Re: QGraphicsRectItem and QGraphicsTextItem.
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!