Clickable Pixmap class, problems with its parent
-
Hi!
I am trying to make a Clickable Pixmap class which objects, once clicked, will open a QFileDialog to let the user choose another image to swap the current one.
Well, I can't really make this work, in particular how to initialize the parent of the class correctly.
Here's the code:
ClickablePixmap.h
#ifndef CLICKABLEPIXMAP_H #define CLICKABLEPIXMAP_H #include<QPixmap> #include <QWidget> #include <Qt> class ClickablePixmap : QPixmap { Q_OBJECT public: explicit ClickablePixmap(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickablePixmap(); void mousePressEvent(QMouseEvent* event); signals: void clicked(); public slots: void ImageClicked(); }; #endif // CLICKABLEPIXMAP_H
ClickablePixmap.cpp
#include "clickablepixmap.h" #include <QFileDialog> #include <QDebug> ClickablePixmap::ClickablePixmap(QWidget* parent, Qt::WindowFlags f) { //how should I initialize the parent? } void ClickablePixmap::ImageClicked() { qDebug()<<"image_clicked"; //how can I put here the parent of this object? (which is supposed to be instead of "this" ) QString filename = QFileDialog::getOpenFileName(this, tr("Choose"), "", tr("Images(*.png *.jpg *.jpeg *.bmp)")); if(QString::compare(filename, QString()) != 0) { QImage img; bool valid = img.load(filename); if(valid) { img = img.scaled(48,48); } else { //error handling } } }
-
Hi!
I am trying to make a Clickable Pixmap class which objects, once clicked, will open a QFileDialog to let the user choose another image to swap the current one.
Well, I can't really make this work, in particular how to initialize the parent of the class correctly.
Here's the code:
ClickablePixmap.h
#ifndef CLICKABLEPIXMAP_H #define CLICKABLEPIXMAP_H #include<QPixmap> #include <QWidget> #include <Qt> class ClickablePixmap : QPixmap { Q_OBJECT public: explicit ClickablePixmap(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickablePixmap(); void mousePressEvent(QMouseEvent* event); signals: void clicked(); public slots: void ImageClicked(); }; #endif // CLICKABLEPIXMAP_H
ClickablePixmap.cpp
#include "clickablepixmap.h" #include <QFileDialog> #include <QDebug> ClickablePixmap::ClickablePixmap(QWidget* parent, Qt::WindowFlags f) { //how should I initialize the parent? } void ClickablePixmap::ImageClicked() { qDebug()<<"image_clicked"; //how can I put here the parent of this object? (which is supposed to be instead of "this" ) QString filename = QFileDialog::getOpenFileName(this, tr("Choose"), "", tr("Images(*.png *.jpg *.jpeg *.bmp)")); if(QString::compare(filename, QString()) != 0) { QImage img; bool valid = img.load(filename); if(valid) { img = img.scaled(48,48); } else { //error handling } } }
@cppStudent012 Hi,
I’m not sure that use Qpixmap derived class is the right choose.
When I have to do this use QWidget or its derived classes. -
Hi!
I am trying to make a Clickable Pixmap class which objects, once clicked, will open a QFileDialog to let the user choose another image to swap the current one.
Well, I can't really make this work, in particular how to initialize the parent of the class correctly.
Here's the code:
ClickablePixmap.h
#ifndef CLICKABLEPIXMAP_H #define CLICKABLEPIXMAP_H #include<QPixmap> #include <QWidget> #include <Qt> class ClickablePixmap : QPixmap { Q_OBJECT public: explicit ClickablePixmap(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickablePixmap(); void mousePressEvent(QMouseEvent* event); signals: void clicked(); public slots: void ImageClicked(); }; #endif // CLICKABLEPIXMAP_H
ClickablePixmap.cpp
#include "clickablepixmap.h" #include <QFileDialog> #include <QDebug> ClickablePixmap::ClickablePixmap(QWidget* parent, Qt::WindowFlags f) { //how should I initialize the parent? } void ClickablePixmap::ImageClicked() { qDebug()<<"image_clicked"; //how can I put here the parent of this object? (which is supposed to be instead of "this" ) QString filename = QFileDialog::getOpenFileName(this, tr("Choose"), "", tr("Images(*.png *.jpg *.jpeg *.bmp)")); if(QString::compare(filename, QString()) != 0) { QImage img; bool valid = img.load(filename); if(valid) { img = img.scaled(48,48); } else { //error handling } } }
hi @cppStudent012
QPixamp by default does not inherit QObject and/or QWidget,
-> there's no default Signal/Slot, mouseEvents or even a parent associated to it.You would have to do this inside the Widget that is actually drawing/showing the pixmap
-
Hi
I think something like
https://wiki.qt.io/Clickable_QLabel
will work better than Pixmap as base. -
Hi
I think something like
https://wiki.qt.io/Clickable_QLabel
will work better than Pixmap as base. -
Thank you everyone for answering!
I tried to apply everyone suggestions and here is the final working code:
ClickablePixmap.h
#ifndef CLICKABLEPIXMAP_H #define CLICKABLEPIXMAP_H #include<QLabel> #include<QPixmap> #include <QWidget> #include <Qt> class ClickablePixmap : public QLabel { Q_OBJECT public: explicit ClickablePixmap(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickablePixmap(); void mousePressEvent(QMouseEvent* event); signals: void clicked(); public slots: void ImageClicked(); }; #endif // CLICKABLEPIXMAP_H
ClickablePixmap.cpp
#include "clickablepixmap.h" #include <QFileDialog> ClickablePixmap::ClickablePixmap(QWidget* parent, Qt::WindowFlags f) : QLabel(parent) { QPixmap img(":/Path_to_your_image"); img = img.scaled(48,48); setPixmap(img); } ClickablePixmap::~ClickablePixmap() {} void ClickablePixmap::mousePressEvent(QMouseEvent* event) { ImageClicked(); emit clicked(); } void ClickablePixmap::ImageClicked() { QString filename = QFileDialog::getOpenFileName(this, tr("Choose"), "", tr("Images(*.png *.jpg *.jpeg *.bmp)")); if(QString::compare(filename, QString()) != 0) { QImage img; bool valid = img.load(filename); if(valid) { img = img.scaled(48,48); setPixmap(QPixmap::fromImage(img)); } else { //error handling } } }
Thank you again everyone! :)
P.S. Any further improvement is always welcome. :)
-
hi
I think you missed to send the parent to base class/ call base class constructor?
like
ClickablePixmap::ClickablePixmap(QWidget* parent, Qt::WindowFlags f) : QWidget(parent) {
...
} -
Thank you everyone for answering!
I tried to apply everyone suggestions and here is the final working code:
ClickablePixmap.h
#ifndef CLICKABLEPIXMAP_H #define CLICKABLEPIXMAP_H #include<QLabel> #include<QPixmap> #include <QWidget> #include <Qt> class ClickablePixmap : public QLabel { Q_OBJECT public: explicit ClickablePixmap(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); ~ClickablePixmap(); void mousePressEvent(QMouseEvent* event); signals: void clicked(); public slots: void ImageClicked(); }; #endif // CLICKABLEPIXMAP_H
ClickablePixmap.cpp
#include "clickablepixmap.h" #include <QFileDialog> ClickablePixmap::ClickablePixmap(QWidget* parent, Qt::WindowFlags f) : QLabel(parent) { QPixmap img(":/Path_to_your_image"); img = img.scaled(48,48); setPixmap(img); } ClickablePixmap::~ClickablePixmap() {} void ClickablePixmap::mousePressEvent(QMouseEvent* event) { ImageClicked(); emit clicked(); } void ClickablePixmap::ImageClicked() { QString filename = QFileDialog::getOpenFileName(this, tr("Choose"), "", tr("Images(*.png *.jpg *.jpeg *.bmp)")); if(QString::compare(filename, QString()) != 0) { QImage img; bool valid = img.load(filename); if(valid) { img = img.scaled(48,48); setPixmap(QPixmap::fromImage(img)); } else { //error handling } } }
Thank you again everyone! :)
P.S. Any further improvement is always welcome. :)
@cppStudent012
by making QWidget the base class and a QLabel the member, you make it more complicated than it has to be.This way you'll have to apply a Layout or override the resizeEvent to align your QLabel correctly!
-
Both suggested changes applied