Fit image in QGraphicsView and QGraphicsScene



  • Hello friends, i am doing my final project for my bachelor degree and still i ignore too much Qt.

    I am trying to fit a image to my GraphicView in my windows but my result is this:

    https://ibb.co/bxHTAJ

    image

    how you can see, the image isn't fitting to my frame. I found a lot of solutions in internet but anything is working to my project. Which is the problem?

    My code:

    CPP

    #include "interfazmatrizexperto.h"
    #include "ui_interfazmatrizexperto.h"
    using namespace std;
    
    interfazMatrizExperto::interfazMatrizExperto(const QString rutaImagenFondo, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::interfazMatrizExperto)
    {
        ui->setupUi(this);
    
        //Imagen de fondo
        QPixmap bkgnd("../InterfazKinect/img/foto_index_sinTXT.png");
        bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
        QPalette palette;
        palette.setBrush(QPalette::Background, bkgnd);
        this->setPalette(palette);
    
       /* Here i am trying to display the picture */
        scene = new QGraphicsScene(this);
        scene->setSceneRect(ui->mostrarImagen->rect());
        scene->addPixmap(QPixmap(rutaImagenFondo));
        ui->mostrarImagen->setScene(scene);
        ui->mostrarImagen->show();
    
    
    
    
        /*
        labelImage = new subQLabel(this);
        labelImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        QPixmap pix(rutaImagenFondo);
        int w = ui->labelImage->width();
        int h = ui->labelImage->height();
        ui->labelImage->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
        ui->labelImage->setAlignment(Qt::AlignCenter);*/
    
    }
    
    interfazMatrizExperto::~interfazMatrizExperto()
    {
        delete ui;
    }
    
    void interfazMatrizExperto::mousePressEvent(QMouseEvent *event)
    {
    
        QWidget::mousePressEvent(event);
    
    }
    
    void interfazMatrizExperto::resizeEvent(QResizeEvent *evt)
    {
        QPixmap bkgnd("../InterfazKinect/img/foto_index_sinTXT.png");//Load pic
        bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);//set scale of pic to match the window
        QPalette p = palette(); //copy current, not create new
        p.setBrush(QPalette::Background, bkgnd);//set the pic to the background
        setPalette(p);//show the background pic
    
        QWidget::resizeEvent(evt); //call base implementation
    }
    
    void subQLabel::mousePressEvent(QMouseEvent *event)
    {
    
    
        cout << "Ha pulsado" << endl;
        int x = event->x();
        int y = event->y();
    
        cout << "punto x: " << x << " punto y: " << y << endl;
    
    }
    
    subQLabel::subQLabel(QWidget* parent) : QLabel(parent)
    {
    
    }
    
    subQLabel::~subQLabel()
    {
    
    
    }
    
    void interfazMatrizExperto::showEvent(QShowEvent *) {
         ui->mostrarImagen->fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
    }
    
    void interfazMatrizExperto::on_terminarBottom_clicked()
    {
    
    }
    
    void interfazMatrizExperto::on_deshacerbottom_clicked()
    {
    
    }
    
    void interfazMatrizExperto::on_comebackbottom_clicked()
    {
    
    }
    
    

    H

    #ifndef INTERFAZMATRIZEXPERTO_H
    #define INTERFAZMATRIZEXPERTO_H
    
    #include <QWidget>
    #include <QMouseEvent>
    #include <QImage>
    #include <QLabel>
    #include <QScrollArea>
    #include <iostream>
    #include <QPixmap>
    #include <QVBoxLayout>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QGraphicsPixmapItem>
    
    class subQLabel;
    class GraphicView;
    
    namespace Ui {
    class interfazMatrizExperto;
    }
    
    class interfazMatrizExperto : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit interfazMatrizExperto(const QString rutaImagenFondo,QWidget *parent = 0);
        ~interfazMatrizExperto();
    protected:
        void mousePressEvent(QMouseEvent *event) override;
        void resizeEvent(QResizeEvent *evt);
    private slots:
        void on_terminarBottom_clicked();
    
        void on_deshacerbottom_clicked();
    
        void on_comebackbottom_clicked();
    
        void showEvent(QShowEvent *);
    
    private:
        Ui::interfazMatrizExperto *ui;
        subQLabel *labelImage;
        QGraphicsView *mostrarImagen;
        QGraphicsScene *scene;
        QGraphicsPixmapItem *item;
    };
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    class subQLabel : public QLabel
    {
        Q_OBJECT
    
    public:
        subQLabel(QWidget* parent);
        ~subQLabel();
        void mousePressEvent(QMouseEvent *event) override;
    };
    
    
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    #endif // INTERFAZMATRIZEXPERTO_H
    
    

  • Lifetime Qt Champion

    Hi,

    You shouldn't use the size of a widget in its own constructor. The widget being not yet visible, the value won't make sense.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.