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. setPixmap() doesn't show image!
Forum Updated to NodeBB v4.3 + New Features

setPixmap() doesn't show image!

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.4k Views
  • 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
    David406
    wrote on last edited by David406
    #1

    Hi all, it's me again!
    I was trying to use setPixmap() method to paint an image to a label,
    which is called "ScreenLabel" derived from QLabel, but met a problem. Here is my code:

    void ScreenLabel::paintImageToLabel(const QImage &image, QPoint topLeftPosition)
    {
        selectedImage = image;
    
        QPixmap imageToPix = QPixmap::fromImage(image);
        this->resize(imageToPix.size());
        //this->setFixedSize(imageToPix.size());
        this->setPixmap(imageToPix);
        QString filename = "D:\\testhh.png";
        this->pixmap()->save(filename);
        this->move(topLeftPosition);
        this->show();
    }
    

    And I called this method in my main widget class "ScreenShot" to paint selected image of the background into that label:

    void ScreenShot::setSelectedImageToLabel(QPoint topLeftPoint, int w, int h)
    {
        QImage selectedImage = background.copy(topLeftPoint.x(), topLeftPoint.y(), w, h);
        imageLabel->paintImageToLabel(selectedImage, topLeftPoint);
    }
    

    where "background" is a QImage type storing image information.

    But this method didn't work, all I got is a white area without nothing painted,
    namely the copied image was not set to the label pixmap.

    alt text

    So I tried to debug by saving the label's pixmap to my local disk using
    this->pixmap()->save(filename);
    as in the above code. And this turned out to be right, there's nothing wrong
    with the saved pixmap of this label, namely the selected area of the background image.
    Here is the saved pixmap "testhh.png":

    alt text

    Here are the declarations of my main widget "ScreenShot" and label "ScreenLabel":

    class ScreenLabel : public QLabel
    {
        Q_OBJECT
    
    public:
        ScreenLabel(QWidget *);
        ... ...
    private:
        ... ...
    signals:
        ... ...
    public slots:
        ... ...
    }
    
    
    class ScreenShot : public QWidget
    {
        Q_OBJECT
    
    public:
        ScreenShot(QWidget *parent = 0);
        ... ...
        ... ...
    }
    

    The ScreenLabel instance is created in the ScreenShot constructor as follows:

    imageLabel = new ScreenLabel(this);
    

    where the ScreenLabel constructor is defined as:

    ScreenLabel::ScreenLabel(QWidget */*parent*/)
        :
          /* QLabel(parent), */
         isPressed(false),
        ... ...
    {
        ... ...
        ... ...
    }
    

    Here I commented out the QLabel(parent) initialization, this way I can get the above resulting pictures. However if I leave it there without commented out, I wouldn't even be able to get the white area (the first picture), namely nothing will be shown.

    So what is the problem here, any ideas?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      David406
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Could you try with a plain QLabel, no subclass?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        D 1 Reply Last reply
        2
        • VRoninV VRonin

          Could you try with a plain QLabel, no subclass?

          D Offline
          D Offline
          David406
          wrote on last edited by
          #4

          @VRonin
          Thanks!
          When I try with QLabel directly, it works!
          So could you please so kind to tell me what's
          the problem here!

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Could you post the code of your subclass of QLabel?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Locked continued here: https://forum.qt.io/topic/78128/qlabel-setpixmap-doesn-t-show-image

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              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