Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QPainter::setTransform in paint() in Qt?

How to use QPainter::setTransform in paint() in Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 644 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tushu
    wrote on 12 Apr 2022, 10:44 last edited by tushu 4 Dec 2022, 17:29
    #1

    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.

    T J 2 Replies Last reply 12 Apr 2022, 15:00
    0
    • T tushu
      12 Apr 2022, 10:44

      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.

      T Offline
      T Offline
      tushu
      wrote on 12 Apr 2022, 15:00 last edited by tushu 4 Dec 2022, 17:19
      #2

      @SGaist @Christian Ehrlicher @JonB Can you help me in checking my paint() ?

      1 Reply Last reply
      0
      • T tushu
        12 Apr 2022, 10:44

        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.

        J Offline
        J Offline
        JonB
        wrote on 12 Apr 2022, 18:30 last edited by JonB 4 Dec 2022, 18:32
        #3

        @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());. Or save()/restore(). I don't know if that is an issue or not.

        T 1 Reply Last reply 13 Apr 2022, 03:07
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Apr 2022, 19:02 last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply 13 Apr 2022, 03:30
          1
          • J JonB
            12 Apr 2022, 18:30

            @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());. Or save()/restore(). I don't know if that is an issue or not.

            T Offline
            T Offline
            tushu
            wrote on 13 Apr 2022, 03:07 last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • S SGaist
              12 Apr 2022, 19:02

              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.

              T Offline
              T Offline
              tushu
              wrote on 13 Apr 2022, 03:30 last edited by tushu
              #6

              @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.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 13 Apr 2022, 19:46 last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                4/7

                12 Apr 2022, 19:02

                • Login

                • Login or register to search.
                4 out of 7
                • First post
                  4/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved