Results 1 to 7 of 7

Thread: QScrollArea display custom QLabel

  1. #1
    Join Date
    Apr 2006
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default QScrollArea display custom QLabel

    Hi all,

    I have a custom QLabel m_opImageViewLabel which I reimplement the paintEvent for my display.

    Then I have a QWidget which is used to display the Qlabel.
    However, I want this QLabel to be inside the QScrollArea so that the scrollbar will appear automatically.

    Below is the constructor of the QWidget.

    ImageViewClient::ImageViewClient(QWidget *parent)
    : QWidget(parent)
    {
    int size = 800;

    ui.setupUi(this);
    m_opImageViewLabel = new ImageViewLabel();

    m_poScrollArea = new QScrollArea(this);

    m_poScrollArea->setBackgroundRole(QPalette:ark);
    m_poScrollArea->setMinimumSize(QSize(size,size));
    m_poScrollArea->setMaximumSize(QSize(size,size));

    m_poScrollArea->setWidget(m_opImageViewLabel);

    this->layout()->addWidget(m_poScrollArea);
    }

    When I try to run this, the image does not appear in the label. The Dark background is shown instead.

    But it will work if I create a QLabel directly and add it in.

    QLabel* test = new QLabel();
    test->setPixmap(QPixmap("c:\\testdata\\wj2.bmp"));
    m_poScrollArea->setWidget(test);

    Is there anything I did wrong?

    Thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea display custom QLabel

    Quote Originally Posted by spawnwj View Post
    I have a custom QLabel m_opImageViewLabel which I reimplement the paintEvent for my display.
    Maybe the problem lies in the reimplemented paintEvent()? Do you paint the image or do you pass the event to the base class implementation?
    J-P Nurmi

  3. #3
    Join Date
    Apr 2006
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea display custom QLabel

    Quote Originally Posted by jpn View Post
    Maybe the problem lies in the reimplemented paintEvent()? Do you paint the image or do you pass the event to the base class implementation?
    I should have left this out.
    But how do I pass the event to the base class implementation?
    Thanks

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea display custom QLabel

    Quote Originally Posted by spawnwj View Post
    But how do I pass the event to the base class implementation?
    Qt Code:
    1. void MyLabel::paintEvent(QPaintEvent* event)
    2. {
    3. // let QLabel implementation paint the image
    4. QLabel::paintEvent(event);
    5.  
    6. // do some custom drawing here...
    7. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Apr 2006
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea display custom QLabel

    Quote Originally Posted by jpn View Post
    Qt Code:
    1. void MyLabel::paintEvent(QPaintEvent* event)
    2. {
    3. // let QLabel implementation paint the image
    4. QLabel::paintEvent(event);
    5.  
    6. // do some custom drawing here...
    7. }
    To copy to clipboard, switch view to plain text mode 
    Thanks.
    I have tried to add the paintEvent in but my image is still missing in the scrollbar area.

    This is my custom QLabel.

    ImageViewLabel::ImageViewLabel()
    : QLabel()
    {
    QImage img("c:\\background.bmp" , "bmp");
    m_poImage = img;
    }

    ImageViewLabel::~ImageViewLabel()
    {

    }

    void ImageViewLabel:aintEvent ( QPaintEvent * event )
    {
    QLabel:aintEvent(event);

    QPainter paint(this);
    paint.drawImage(0, 0, m_poImage);
    }

    void ImageViewLabel::setImageInView(QImage image)
    {
    m_poImage = image;
    update();
    }

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea display custom QLabel

    What's the difference to the default QLabel then? Although QLabel uses QPixmap, it supports all the functionality ImageViewLabel has. Are there any threads involved? In that case QImage might be the correct choice as it supports manipulating outside GUI thread. But you can still use the QLabel's built-in capabilities to show the image.

    In your case when the image has not actually been set on the QLabel, QLabel::paintEvent() neither does actually paint anything (although it could draw the frame if any had been set). The reason why can't you see anything is that the ImageViewLabel doesn't request enough size for itself. If you would set the image on the QLabel, the QLabel::sizeHint() would return appropriate size hint and the label would get laid and resized correctly. Now when the QLabel doesn't actually have any content (since the image is put just as a member variable which QLabel implementation is not aware of) it doesn't request for proper size either. The simplest solution to get anything to shown is to reimplement sizeHint() and return the size of the image.
    J-P Nurmi

  7. #7
    Join Date
    Apr 2006
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea display custom QLabel

    Quote Originally Posted by jpn View Post
    What's the difference to the default QLabel then? Although QLabel uses QPixmap, it supports all the functionality ImageViewLabel has. Are there any threads involved? In that case QImage might be the correct choice as it supports manipulating outside GUI thread. But you can still use the QLabel's built-in capabilities to show the image.

    In your case when the image has not actually been set on the QLabel, QLabel:aintEvent() neither does actually paint anything (although it could draw the frame if any had been set). The reason why can't you see anything is that the ImageViewLabel doesn't request enough size for itself. If you would set the image on the QLabel, the QLabel::sizeHint() would return appropriate size hint and the label would get laid and resized correctly. Now when the QLabel doesn't actually have any content (since the image is put just as a member variable which QLabel implementation is not aware of) it doesn't request for proper size either. The simplest solution to get anything to shown is to reimplement sizeHint() and return the size of the image.
    Thanks for your advice. It is due to the sizeHint() like you mention.
    I reimplement the sizeHint and it works now.

Similar Threads

  1. Replies: 2
    Last Post: 8th October 2006, 21:14
  2. Replies: 7
    Last Post: 20th September 2006, 15:45
  3. Replies: 5
    Last Post: 16th May 2006, 21:38
  4. size issues for custom QWidget in QScrollArea
    By anotheruser in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2006, 15:52
  5. Replies: 3
    Last Post: 12th April 2006, 09:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.