Solved I cannot show image in a subclass of QLabel
-
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
-
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));
-
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!
-
@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.
-
@AdrianCruz
Hi, Friend, welcome.
About ,how to set the picture show in the middle of widget. You can try to set
QVBoxLayout or QHBoxLayout
inQWidget
. and, layout toaddWidget(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); }
-
@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!