Resizing QGraphicsView as per the image loaded and keeping the aspect ratio at all times

久未见 提交于 2021-02-10 07:56:35

问题


I have 3 QGraphicsView and 1 QOpenGLWidget laid out in QGridLayout in the center surrounded by other widgets on all 3 sides as shown in the pic below:

All my widgets can be resized using QSplitter around them. As shown in the pic below:

What do i want:

QGraphicsView should always maintain a fixed size equivalent to image size. On resizing using QSplitter the view should resize based on the aspect ratio of the image but not the image itself as we can compare in the pics.

The height and width of the widget should be calculated based on the width and height of the image.

For instance an image with 640px by 480 px should always maintain the dimensions at all times.

What have i done:

I am using Qt5.7 on Win 10 PC. Here is a MVCE example built to show. I used Qt Designer to promote widgets to my own class ImageView

ImageView.h

// qt
#include <QGraphicsView>
#include <QMouseEvent>
#include <QWidget>
#include <QGraphicsScene>
#include <QResizeEvent>
#include <QShowEvent>
#include <QImage>


class ImageView : public QGraphicsView
{
    Q_OBJECT

public:

    ImageView(QWidget* parent = nullptr);

    void setImage(const QImage& image);

    QGraphicsScene* getScene() const;

protected:

    virtual void mouseMoveEvent(QMouseEvent *event) override;
    virtual void resizeEvent(QResizeEvent *event) override;

private:

    QGraphicsScene* m_scene;

    QImage m_image;
};

ImageView.cpp

#include "imageview.h"
#include <QGraphicsPixmapItem>


ImageView::ImageView(QWidget* parent) :
    QGraphicsView{parent},
    m_scene{new QGraphicsScene(this)}
{
    setScene(m_scene);

    setMouseTracking(true);
}

void ImageView::setImage(const QImage &image)
{
    m_image = image;

    const QPixmap& pixImage = QPixmap::fromImage(image);

    m_scene->addPixmap(pixImage);
}

QGraphicsScene* ImageView::getScene() const
{
    return m_scene;
}

void ImageView::mouseMoveEvent(QMouseEvent *event)
{
    //show the pixel values of the image under the mouse
    if(QGraphicsPixmapItem *item = qgraphicsitem_cast<QGraphicsPixmapItem *>(itemAt(event->pos())))
    {
         QPointF p = item->mapFromScene(mapToScene(event->pos()));
         QPoint pixel_pos = p.toPoint();
         QString toolTipText = "x=" + QString::number(pixel_pos.x()) + "," + " y=" + QString::number(pixel_pos.y());
         setToolTip(toolTipText);
    }

    QGraphicsView::mousePressEvent(event);
}

void ImageView::resizeEvent(QResizeEvent *event)
{
   fitInView(m_scene->itemsBoundingRect(),Qt::KeepAspectRatio);

   QGraphicsView::resizeEvent(event);
}

MainWindow.h

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->graphicsView_2->setImage(QImage(":/images/download.jpg"));
    ui->graphicsView->setImage(QImage(":/images/656780.png"));
    ui->graphicsView_3->setImage(QImage(":/images/download_2.jpg"));
}

MainWindow::~MainWindow()
{
    delete ui;
}

来源:https://stackoverflow.com/questions/60804017/resizing-qgraphicsview-as-per-the-image-loaded-and-keeping-the-aspect-ratio-at-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!