setPixmap() doesn't show image!



  • 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?



  • This post is deleted!


  • Could you try with a plain QLabel, no subclass?



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



  • Could you post the code of your subclass of QLabel?




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.