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 can I scale QGraphicsItems?
QtWS25 Last Chance

How can I scale QGraphicsItems?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.3k 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.
  • C Offline
    C Offline
    Ceng0
    wrote on last edited by
    #1

    Hi , how can i scale my QGraphicsItem? I have multiple items. And i would like to scale these QGraphicsItems, something like zoom.. QGraphicsView::fitInView not working. How can i do?
    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QGraphicsPixmapItem>
    #include <QGraphicsScene>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
        QGraphicsPixmapItem *pixmapItem;
        QGraphicsScene *scene;
        void addToScene(QGraphicsItem*);
    
    private:
        Ui::Widget *ui;
    };
    #endif // WIDGET_H
    
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
        scene = new QGraphicsScene;
        pixmapItem = new QGraphicsPixmapItem(QPixmap(/*directory*/)); 
       // more graphicsitems will be added
        ui->graphicsView->setScene(scene);
        addToScene(pixmapItem);
    
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::addToScene(QGraphicsItem *item){
        scene->addItem(item);
        item->setPos(0,0);
    }
    

    Current Positions - graphicsView is fixed. Paint skills :(
    Screenshot_3.png

    The situation I want
    Screenshot_4.png

    JonBJ 1 Reply Last reply
    0
    • C Ceng0

      Hi , how can i scale my QGraphicsItem? I have multiple items. And i would like to scale these QGraphicsItems, something like zoom.. QGraphicsView::fitInView not working. How can i do?
      widget.h

      #ifndef WIDGET_H
      #define WIDGET_H
      
      #include <QWidget>
      #include <QGraphicsPixmapItem>
      #include <QGraphicsScene>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class Widget; }
      QT_END_NAMESPACE
      
      class Widget : public QWidget
      {
          Q_OBJECT
      
      public:
          Widget(QWidget *parent = nullptr);
          ~Widget();
          QGraphicsPixmapItem *pixmapItem;
          QGraphicsScene *scene;
          void addToScene(QGraphicsItem*);
      
      private:
          Ui::Widget *ui;
      };
      #endif // WIDGET_H
      
      #include "widget.h"
      #include "ui_widget.h"
      
      Widget::Widget(QWidget *parent)
          : QWidget(parent)
          , ui(new Ui::Widget)
      {
          ui->setupUi(this);
          scene = new QGraphicsScene;
          pixmapItem = new QGraphicsPixmapItem(QPixmap(/*directory*/)); 
         // more graphicsitems will be added
          ui->graphicsView->setScene(scene);
          addToScene(pixmapItem);
      
      }
      
      Widget::~Widget()
      {
          delete ui;
      }
      
      void Widget::addToScene(QGraphicsItem *item){
          scene->addItem(item);
          item->setPos(0,0);
      }
      

      Current Positions - graphicsView is fixed. Paint skills :(
      Screenshot_3.png

      The situation I want
      Screenshot_4.png

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ceng0
      A QGraphsItem can be scaled via https://doc.qt.io/qt-5/qgraphicsitem.html#setScale

      Or your QPixmaps can be scaled via https://doc.qt.io/qt-5/qpixmap.html#scaled

      QGraphicsView::fitInView() does one particular scaling across all graphics items. The whole view's scale can be set via https://doc.qt.io/qt-5/qgraphicsview.html#scale.

      1 Reply Last reply
      1
      • Ketan__Patel__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3

        Just Simply use fitInView Function like this

            scene->addItem(YOUR_ITEM);
            ui->graphicsView->fitInView(scene->sceneRect(), Qt::IgnoreAspectRatio);
            ui->graphicsView->setScene(scene);
            ui->graphicsView->setRenderHints(QPainter::SmoothPixmapTransform);
            ui->graphicsView->viewport()->update();
            ui->graphicsView->update();
            ui->graphicsView->show();
        
        JonBJ 1 Reply Last reply
        0
        • Ketan__Patel__0011K Ketan__Patel__0011

          Just Simply use fitInView Function like this

              scene->addItem(YOUR_ITEM);
              ui->graphicsView->fitInView(scene->sceneRect(), Qt::IgnoreAspectRatio);
              ui->graphicsView->setScene(scene);
              ui->graphicsView->setRenderHints(QPainter::SmoothPixmapTransform);
              ui->graphicsView->viewport()->update();
              ui->graphicsView->update();
              ui->graphicsView->show();
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Ketan__Patel__0011
          @Ceng0 wrote:

          QGraphicsView::fitInView not working

          I took that to mean that he did not like the result he got for that, and wanted more control over individual item scaling/zooming.

          But maybe you're right and he just meant that he could not get it to work?

          I use fitInView(), it suits me. But in your code could you tell me:

          • Why do you need to do ui->graphicsView->viewport()->update()? I do not do that.

          • What does setRenderHints(QPainter::SmoothPixmapTransform) achieve? I have a map pixmap filling the whole scene, not individual shape/pixmap objects, what is your render hint used for, does it not affect me if all I do is zoom a background full-scene pixmap?

          Ketan__Patel__0011K 1 Reply Last reply
          0
          • JonBJ JonB

            @Ketan__Patel__0011
            @Ceng0 wrote:

            QGraphicsView::fitInView not working

            I took that to mean that he did not like the result he got for that, and wanted more control over individual item scaling/zooming.

            But maybe you're right and he just meant that he could not get it to work?

            I use fitInView(), it suits me. But in your code could you tell me:

            • Why do you need to do ui->graphicsView->viewport()->update()? I do not do that.

            • What does setRenderHints(QPainter::SmoothPixmapTransform) achieve? I have a map pixmap filling the whole scene, not individual shape/pixmap objects, what is your render hint used for, does it not affect me if all I do is zoom a background full-scene pixmap?

            Ketan__Patel__0011K Offline
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011
            wrote on last edited by Ketan__Patel__0011
            #5

            @JonB

            He Can use QWheelEvent For Zooming And Scaling

            @JonB said in How can I scale QGraphicsItems?:

            Why do you need to do ui->graphicsView->viewport()->update()? I do not do that.

            I am using QGraphicsView in My Threading Function And i am display QPixmaps continuously And Displaying
            That's Why I Have to update My QGraphicsView viewport And For Avoid Flickering i am use setRenderhints For Smooth Pixmap Transformation

            JonBJ 1 Reply Last reply
            0
            • Ketan__Patel__0011K Ketan__Patel__0011

              @JonB

              He Can use QWheelEvent For Zooming And Scaling

              @JonB said in How can I scale QGraphicsItems?:

              Why do you need to do ui->graphicsView->viewport()->update()? I do not do that.

              I am using QGraphicsView in My Threading Function And i am display QPixmaps continuously And Displaying
              That's Why I Have to update My QGraphicsView viewport And For Avoid Flickering i am use setRenderhints For Smooth Pixmap Transformation

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Ketan__Patel__0011
              OK, thank you for answering. I don't understand why that requires viewport()->update(). For my case I do not see any flickering, so maybe I don't need the SmoothPixmapTransform.

              Ketan__Patel__0011K 1 Reply Last reply
              0
              • JonBJ JonB

                @Ketan__Patel__0011
                OK, thank you for answering. I don't understand why that requires viewport()->update(). For my case I do not see any flickering, so maybe I don't need the SmoothPixmapTransform.

                Ketan__Patel__0011K Offline
                Ketan__Patel__0011K Offline
                Ketan__Patel__0011
                wrote on last edited by
                #7

                @JonB

                So Whenever you will face any problem like this then try this way

                Thank You

                1 Reply Last reply
                0

                • Login

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