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. How to Get Selected QLabel's Pixmap?

How to Get Selected QLabel's Pixmap?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 4.6k 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.
  • R_IrudezuR Offline
    R_IrudezuR Offline
    R_Irudezu
    wrote on last edited by R_Irudezu
    #1

    I'm trying to select a QLabel and get its pixmap. Subclassed QLabel:
    qlabelevent.h

    #ifndef QLABELEVENT_H
    #define QLABELEVENT_H
    #include <QWidget>
    #include <QLabel>
    #include <QMouseEvent>
    
    class qlabelEvent : public QLabel
    {
        Q_OBJECT
    public:
        explicit qlabelEvent(QWidget *parent);
    
        void mousePressEvent(QMouseEvent *pressEvent);
    
    signals:
    
        void Mouse_Pressed();
    
    public slots:
    };
    
    #endif // QLABELEVENT_H
    

    qlabelevent.cpp:

    #include "qlabelevent.h"
    #include <QWidget>
    
    qlabelEvent::qlabelEvent(QWidget *parent) : QLabel(parent)
    {
    
    }
    
    void qlabelEvent::mousePressEvent(QMouseEvent *pressEvent)
    {
        emit Mouse_Pressed();
    }
    

    mainwindow.h private slot:

    private slots:
        void Mouse_Pressed();
    

    mainwindow.cpp connect and Mouse_Pressed() function:

    //in the constructor
    connect(ui->mouseEventLabel, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
    
    void MainWindow::Mouse_Pressed()
    {
        const QPixmap *pixMap = ui->mouseEventLabel->pixmap();
        ui->testoLabel->setPixmap(*pixMap);
    }
    

    I just want to select a label in the UI (there is many of them so i can not do just like the above function).
    Any idea how to do?

    Keizoku wa chikaranari.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Question is slightly confused me. Still let me try. If your question is how to get the pixmap from label you have to use the pixmap(..) function only. You have no other choice. Your code is already doing. So what is the real question. May be you are trying asking something different.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      R_IrudezuR 1 Reply Last reply
      1
      • dheerendraD dheerendra

        Question is slightly confused me. Still let me try. If your question is how to get the pixmap from label you have to use the pixmap(..) function only. You have no other choice. Your code is already doing. So what is the real question. May be you are trying asking something different.

        R_IrudezuR Offline
        R_IrudezuR Offline
        R_Irudezu
        wrote on last edited by
        #3

        @dheerendra I am creating 100 labels and each one has image, so has pixmaps. The question is, is it possible when i pressed any QLabel (in these 100 labels), can i get its pixmap.

        My code is working for one and specific QLabel. Problem is, when i clicked a label, how i know which label it is.

        Keizoku wa chikaranari.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Ok. What you are doing is right. Define another signal inside the qlabelevent class called mytext(..).

          signals :
          void mytext(QString text)

          void MainWindow::Mouse_Pressed()
          {
          const QPixmap *pixMap = ui->mouseEventLabel->pixmap();
          ui->testoLabel->setPixmap(*pixMap);
          emit mytext(this->text())
          }

          You can use the mytext(..) signal wherever you want.

          Why do you want to identify which label ? What are you doing with that ? Based on this I can suggest you to do.

          There is another way to identify the label. You can use the objectName().. as well.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          R_IrudezuR 1 Reply Last reply
          0
          • dheerendraD dheerendra

            Ok. What you are doing is right. Define another signal inside the qlabelevent class called mytext(..).

            signals :
            void mytext(QString text)

            void MainWindow::Mouse_Pressed()
            {
            const QPixmap *pixMap = ui->mouseEventLabel->pixmap();
            ui->testoLabel->setPixmap(*pixMap);
            emit mytext(this->text())
            }

            You can use the mytext(..) signal wherever you want.

            Why do you want to identify which label ? What are you doing with that ? Based on this I can suggest you to do.

            There is another way to identify the label. You can use the objectName().. as well.

            R_IrudezuR Offline
            R_IrudezuR Offline
            R_Irudezu
            wrote on last edited by
            #5

            @dheerendra I don't want to emit text Mr. Dheerendra. I am making a simple gallery and i want to transfer pixmap to another qlabel.

            Just tried a simple visual:
            0_1542081720238_Untitled.png

            When a label (in the right side) clicked, i just want to set its image to label at left side.
            Btw i created labels with code, not with ui.

            Keizoku wa chikaranari.

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              see if this simple example helps.

              MyWidget::MyWidget(QWidget *parent)
              : QWidget(parent)
              {
              this->m_lab = new MyLabel;
              this->m_lab2 = new MyLabel;
              this->m_lab->setPixmap(QPixmap("/Users/dheeru/myImage.png"));

              this->hlyt = new QHBoxLayout(this);
              this->hlyt->addWidget(m_lab);
              this->hlyt->addWidget(m_lab2);
              connect(m_lab,SIGNAL(mypix(const QPixmap*)),this,SLOT(changeMyPix(const QPixmap*)));
              

              }

              void MyWidget::changeMyPix(const QPixmap* pix){
              this->m_lab2->setPixmap(*pix);
              }
              ======= Custom Label =============
              MyLabel::MyLabel()
              {

              }

              void MyLabel::mousePressEvent(QMouseEvent *ev)
              {
              emit mypix(this->pixmap());
              }

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

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

                Hi
                Just as a note.
                a QListWidget in icon mode would most likely work better than tons of labels.

                alt text

                R_IrudezuR 1 Reply Last reply
                6
                • mrjjM mrjj

                  Hi
                  Just as a note.
                  a QListWidget in icon mode would most likely work better than tons of labels.

                  alt text

                  R_IrudezuR Offline
                  R_IrudezuR Offline
                  R_Irudezu
                  wrote on last edited by R_Irudezu
                  #8

                  Hit the bull's-eye @mrjj this actually what i want to do and completely appropriate for my demand. So let me write here how i did a quite simple example:

                  ///in the constructor
                  QDirIterator it("/ImageDirectory", QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
                  
                  for(int i = 0; i < 200; i++)
                  {
                      QString fileName = it.next();
                      QListWidgetItem *item = new QListWidgetItem(QIcon(fileName ), NULL); 
                      ui->listWidget->addItem(item);
                  }
                  
                  //in the itemClicked slot
                  void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
                  {
                      QPixmap pixmapFromIcon = item->icon().pixmap(item->icon().actualSize(QSize(70,70)));
                      ui->testoLabel->setPixmap(pixmapFromIcon);
                  }
                  

                  Thank you @dheerendra and @mrjj

                  I couldn't marked as answer your answer @mrjj, it wasn't display in dropdown menu, maybe it might be a bug, here is a screenshot:

                  0_1542162623995_Capture.PNG

                  Keizoku wa chikaranari.

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

                    hi
                    Good to hear :)
                    Yep, im not sure what is up "correct answer"
                    i only get it on my own posts, if I start the thread
                    so not sure it works as intended.

                    alt text

                    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