How to use QPainter::setTransform in paint() in Qt?
-
I have a simple QGraphicsView with text written over it.That text is wrapped under a bounding rect. I have truncated a text using QFontMetrics::elidedText.
When initially view gets loaded, it shows all truncated text properly. But when I zoom-in only few truncated text gets visible. If for few more times I zoomed-in, then some times it shows all the truncated text and some times , it never shows any truncated text.
Before truncated text code gets added, zoom-in was working properly.widget.cpp
void widget::ZoomIn() { double scaleFactor = 1.1; view->scale(scaleFactor,scaleFactor); } void Widget::on_textButton_clicked() { //logic to get string s and its co-ordinates firstPos and secondPos Text* t = new Text() ; QGraphicsTextItem* text = t->drawText(s,firstPos,secondPos,50,40); scene->addItem(text); }
Text.cpp
Text::Text(const QString &text,int left,int top,int width,int height) : QGraphicsTextItem (text),currentString(text), Left(left),Top(top),Width(width),Height(height) {} Text::Text(){} QGraphicsTextItem *Text::drawText(QString s, int left,int top,int width,int height) { QGraphicsTextItem *text = new Text(s,left,top,width,height); return text; } void Text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); QFont defaultFont = QApplication::font(); QFontMetrics fm = QFontMetrics(defaultFont); QRect rect = QRect(Left,Top,Width,Height); QString elidedText = fm.elidedText(currentString, Qt::ElideLeft,rect.width()); QTransform trans = v->transform(); QTransform prevTrans; painter->setTransform(trans); painter->drawText(rect,Qt::AlignRight,elidedText); painter->setTransform(prevTrans); }
I understood that, in zoom-in I am changing transformation and painter is drawing on old transformation. So I have to take transformation from view and set it with painter then drawText() and then set painter transformation as previous transformation.
I did , but still problem persist. -
@tushu said in How to use QPainter::setTransform in paint() in Qt?:
QTransform trans = v->transform(); QTransform prevTrans; painter->setTransform(trans); painter->drawText(rect,Qt::AlignRight,elidedText); painter->setTransform(prevTrans);
I think you should go
QTransform prevTrans(painter->transform());
. Orsave()
/restore()
. I don't know if that is an issue or not. -
Hi,
What is v is that function ?
That said, the design of that class is strange, what is the goal of that drawText method ? Beside having a misleading name, it's just a factory method that calls the class constructor with parameters so basically, you create an instance of your class to create a different instance of the same class. And you do not delete the original object so each time you call on_textButton_clicked you leak one object.
On a side note, you should first concentrate on just painting your elided text. The view handles the transform so the painter your receive will likely already have it applied.
Finally, since you draw a simple shortened string, you might want to consider using QGraphicsSimpleTextItem or even just QGraphicsRectItem as base class.
-
@JonB I tried what you suggested. But It did not work. I tried with save() and restore() that also did not work. On my local machine, I tried elidedText() code and it worked fine along with proper working of zoom-in and without any Transformation adjustment. But why main code is not working, not understanding. Anyway thanks for your help.
If you have any more suggestion, you are welcome. -
@SGaist That v is basically a view. I am storing a current view transformation. In my code I have changed default selection behaviour of Qt ( after clicking on item it creates a dashed rectangle around item) For that I had to create drawText().
On my local machine, without adjusting transformation , elidedText() code is working fine with zoom-in but in main code it is giving problem. And that's why I am guessing various reasons of code not working.
Thanks for your help. If you have any more suggestion, you are welcome. -
There's no reason for an item to keep a pointer to a view. You can have several views on top of a scene that shows different parts of it. Just check the 40000 chips example.