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. I cannot show image in a subclass of QLabel
Forum Updated to NodeBB v4.3 + New Features

I cannot show image in a subclass of QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 798 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 last edited by
    #1

    Hello! Firstly, sorry for my poor english, i have a problem with my final project and i cannot solve it!

    I have to create a Widget that i show a picture and when you click with the mouse, that have to return the position in the picture. I am using a subclass of QLabel because i saw in videos and in internet differents ways to do that.

    I am able to put a QLabel and link that with the subclass of QLabel, and also in the application output i can see the click points into the label.

    Now i am trying to show the picture in the label but i cannot, i don' know for what. I used pixmap but this doesn't load the picture.

    This is my code:

    interfazmatrizexperto.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 initialize the QLabel subclass and try to load the picture
        labelImage = new subQLabel(0);
        labelImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        QPixmap pm1(rutaImagenFondo);
        labelImage->setPixmap(pm1);
        labelImage->adjustSize();
        labelImage->setScaledContents(true);
    }
    
    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()
    {
    }
    

    interfazmatrizexperto.h

    #ifndef INTERFAZMATRIZEXPERTO_H
    #define INTERFAZMATRIZEXPERTO_H
    
    #include <QWidget>
    #include <QMouseEvent>
    #include <QImage>
    #include <QLabel>
    #include <QScrollArea>
    #include <iostream>
    #include <QPixmap>
    
    class subQLabel;
    
    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:
        Ui::interfazMatrizExperto *ui;
        subQLabel *labelImage;
    };
    
    class subQLabel : public QLabel
    {
        Q_OBJECT
    
    public:
        subQLabel(QWidget* parent);
        ~subQLabel();
        void mousePressEvent(QMouseEvent *event) override;
    };
    
    #endif // INTERFAZMATRIZEXPERTO_H
    
    
    1 Reply Last reply
    0
    • Prince_0912P Offline
      Prince_0912P Offline
      Prince_0912
      wrote on last edited by
      #2

      Hi @AdrianCruz you can try like this,

      QPixmap pix(":/images/xyz.png");

      int w = ui->label->width();

      int h = ui->label->height();

      ui->label->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));

      1 Reply Last reply
      2
      • D Offline
        D Offline
        deleted396
        wrote on last edited by
        #3

        Yes it's working! Thanks so much!

        I have other question if ypu can reply me. When I open full screen, the image show it in the top and left of the windows, Do you know how i can set the picture in the middle?

        thanks so much again!

        joeQJ 1 Reply Last reply
        0
        • Prince_0912P Offline
          Prince_0912P Offline
          Prince_0912
          wrote on last edited by
          #4

          @AdrianCruz in your ui file see the position of QLabel widget and put it in center of MainWidget.

          After get your solution make go topic tool -> Mark as solved.

          Your'e Welcome.

          1 Reply Last reply
          1
          • D deleted396

            Yes it's working! Thanks so much!

            I have other question if ypu can reply me. When I open full screen, the image show it in the top and left of the windows, Do you know how i can set the picture in the middle?

            thanks so much again!

            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

            @AdrianCruz

            Hi, Friend, welcome.

            About ,how to set the picture show in the middle of widget. You can try to set QVBoxLayout or QHBoxLayout in QWidget. and, layout to addWidget(labelObjectPointer).

            like below snippet:

            #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 initialize the QLabel subclass and try to load the picture
            
                labelImage = new subQLabel(0);//Hi,Friend. It is better to use this,  not 0.
            
                labelImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
                QPixmap pm1(rutaImagenFondo);
                labelImage->setPixmap(pm1);
                labelImage->adjustSize();
                labelImage->setScaledContents(true);
            
                QVBoxLayout* vlyt = new QVBoxLayout;
                vlyt->addWidget(labelImage);
                this->setLayout(vlyt);
            }
            

            Just do it!

            D 1 Reply Last reply
            0
            • joeQJ joeQ

              @AdrianCruz

              Hi, Friend, welcome.

              About ,how to set the picture show in the middle of widget. You can try to set QVBoxLayout or QHBoxLayout in QWidget. and, layout to addWidget(labelObjectPointer).

              like below snippet:

              #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 initialize the QLabel subclass and try to load the picture
              
                  labelImage = new subQLabel(0);//Hi,Friend. It is better to use this,  not 0.
              
                  labelImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
                  QPixmap pm1(rutaImagenFondo);
                  labelImage->setPixmap(pm1);
                  labelImage->adjustSize();
                  labelImage->setScaledContents(true);
              
                  QVBoxLayout* vlyt = new QVBoxLayout;
                  vlyt->addWidget(labelImage);
                  this->setLayout(vlyt);
              }
              
              D Offline
              D Offline
              deleted396
              wrote on last edited by
              #6

              @joeQ i tried that but anything happened. Will i need to do something more in my code or just include this:

              QVBoxLayout* vlyt = new QVBoxLayout;
                  vlyt->addWidget(labelImage);
                  this->setLayout(vlyt);
              

              thanks!

              1 Reply Last reply
              0

              • Login

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