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. Fit image in QGraphicsView and QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Fit image in QGraphicsView and QGraphicsScene

Scheduled Pinned Locked Moved Unsolved General and Desktop
pictureqgraphicsviewqgraphicsscene
2 Posts 2 Posters 2.3k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    deleted396
    wrote on 20 Jun 2018, 11:02 last edited by deleted396
    #1

    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
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Jun 2018, 15:41 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      2/2

      20 Jun 2018, 15:41

      • Login

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