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. Clickable Pixmap class, problems with its parent
Qt 6.11 is out! See what's new in the release blog

Clickable Pixmap class, problems with its parent

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 2.3k Views 2 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.
  • cppStudent012C Offline
    cppStudent012C Offline
    cppStudent012
    wrote on last edited by
    #1

    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
            }
        }
    }
    
    
    CP71C J.HilkJ 2 Replies Last reply
    0
    • M Offline
      M Offline
      mahd96
      wrote on last edited by
      #2

      why you need to initialize the parent in mouseEvent? i dont getting the purpose.

      1 Reply Last reply
      1
      • cppStudent012C cppStudent012

        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
                }
            }
        }
        
        
        CP71C Offline
        CP71C Offline
        CP71
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        2
        • cppStudent012C cppStudent012

          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
                  }
              }
          }
          
          
          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          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


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          4
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            I think something like
            https://wiki.qt.io/Clickable_QLabel
            will work better than Pixmap as base.

            J.HilkJ 1 Reply Last reply
            2
            • mrjjM mrjj

              Hi
              I think something like
              https://wiki.qt.io/Clickable_QLabel
              will work better than Pixmap as base.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @mrjj
              that entry should be revised, makes my OCD tingle all over the place ;-)


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • cppStudent012C Offline
                cppStudent012C Offline
                cppStudent012
                wrote on last edited by cppStudent012
                #7

                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. :)

                J.HilkJ 1 Reply Last reply
                1
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  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) {
                  ...
                  }

                  1 Reply Last reply
                  4
                  • cppStudent012C cppStudent012

                    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. :)

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @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!


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    4
                    • cppStudent012C Offline
                      cppStudent012C Offline
                      cppStudent012
                      wrote on last edited by
                      #10

                      Both suggested changes applied

                      1 Reply Last reply
                      1

                      • Login

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