Reputation: 417
I want to change my QGraphicsItem
position by resize event over graphicsview.
I scaled position by newSize/oldSize
but my items stay at the same position.
I don't know what wrong with my code or a better way to change my items position.
bool cameraItems::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Resize) {
QResizeEvent *e = static_cast<QResizeEvent*>(event);
if(obj == ui->graphicsView) {
setFixedSizeForGraphicsView(e->size(), e->oldSize());
ui->graphicsView->scene()->setSceneRect(0, 0, e->size().width(), e->size().height());
ui->graphicsView->setSceneRect(0, 0, e->size().width(), e-->size().height());
}
}
QWidget::eventFilter(obj, event);
}
void cameraItems::setFixedSizeForGraphicsView(QSize size, QSize oldSize)
{
foreach (singleCamera *cam, m_cameras) {
prevImageSize = cam->imageSize();
QPointF ppos = cam->pos();
QPointF newPos = QPointF((ppos.x()/prevSize.width())*size.width(), (ppos.y()/prevSize.height())*size.height());
cam->setPos(newPos);
}
}
Upvotes: 1
Views: 886
Reputation: 32635
You can make your custom class which inherits from QGraphicsView
and then reimplement resizeEvent( QResizeEvent *event )
like:
void MyView::resizeEvent(QResizeEvent *event)
{
QRectF rect = this->scene()->itemsBoundingRect();
fitInView(rect, ,Qt::KeepAspectRatio);
QGraphicsView::resizeEvent(event);
}
This way the view will always display the whole scene. I.e. if the window size is changed and the graphicsView is resized, The scene gets scaled and you can see everything appropriately.
Upvotes: 1