How can I scale QGraphicsItems?
-
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 :(
The situation I want
-
@Ceng0
AQGraphsItem
can be scaled via https://doc.qt.io/qt-5/qgraphicsitem.html#setScaleOr your
QPixmap
s can be scaled via https://doc.qt.io/qt-5/qpixmap.html#scaledQGraphicsView::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. -
Just Simply use
fitInView
Function like thisscene->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();
-
@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?
-
-
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 displayQPixmaps
continuously And Displaying
That's Why I Have to update MyQGraphicsView viewport
And For Avoid Flickering i am usesetRenderhints
For Smooth Pixmap Transformation -
@Ketan__Patel__0011
OK, thank you for answering. I don't understand why that requiresviewport()->update()
. For my case I do not see any flickering, so maybe I don't need theSmoothPixmapTransform
.