PDA

View Full Version : QScrollArea display custom QLabel



spawnwj
21st November 2006, 04:08
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::Dark);
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

jpn
21st November 2006, 09:13
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?

spawnwj
21st November 2006, 09:45
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

jpn
21st November 2006, 09:49
But how do I pass the event to the base class implementation?


void MyLabel::paintEvent(QPaintEvent* event)
{
// let QLabel implementation paint the image
QLabel::paintEvent(event);

// do some custom drawing here...
}

spawnwj
22nd November 2006, 01:42
void MyLabel::paintEvent(QPaintEvent* event)
{
// let QLabel implementation paint the image
QLabel::paintEvent(event);

// do some custom drawing here...
}


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::paintEvent ( QPaintEvent * event )
{
QLabel::paintEvent(event);

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

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

jpn
22nd November 2006, 06:12
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.

spawnwj
6th December 2006, 03:38
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.

Thanks for your advice. It is due to the sizeHint() like you mention.
I reimplement the sizeHint and it works now.