Thanks Lykurg. do you know where can I find an example? if it's not so much trouble![]()
Thanks Lykurg. do you know where can I find an example? if it's not so much trouble![]()
It's always better to try by yourself,but if you really want for that simple task:
Qt Code:
{ public: {} { painter->setPen(Qt::black); painter->drawRect(option->rect); } };To copy to clipboard, switch view to plain text mode
cydside (19th July 2009)
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?
just use qpainter transforms to write vertical text
create a custom QGraphicsItem with both QGraphicsRect and QGraphicsText
and in QGraphicsView class (parent)
QTransform transform;
transform.rotate( 90);
grpahicsItem->setTransform(transform);
"Behind every great fortune lies a crime" - Balzac
thats what i am suggesting .. combine both the text and rectangle into single QGraphicsItem ..but how to obtain a text centered both vetically and horizontally in the rectangle?
paint such that
create a custom QGraphicsItem()
Qt Code:
{ Q_OBJECT public: };To copy to clipboard, switch view to plain text mode
Qt Code:
{ textItem->setPos(x, y); //here u set position of text textItem->setPlainText(message); //set your text here } { painter->drawRect(rect()); }To copy to clipboard, switch view to plain text mode
now the item is ready in horizontal ... now in parent graphicsView() rotate it using previous code ...
"Behind every great fortune lies a crime" - Balzac
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.
yes lykurg .. its a better option .. in paint() itself we can rotate the text and add the text in that custom item ...
"Behind every great fortune lies a crime" - Balzac
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:
Qt 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);To copy to clipboard, switch view to plain text mode
the compiler is not agree with me because:
Qt Code:
note: candidates are: QPainter::QPainter(const QPainter&) note: QPainter::QPainter(QPaintDevice*)To copy to clipboard, switch view to plain text mode
where do i have to link my QPainter object???
thanks guys, I really appreciate your comments!
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:aint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )
"Behind every great fortune lies a crime" - Balzac
cydside (20th July 2009)
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!
cydside (20th July 2009)
Bookmarks