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::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
Re: QScrollArea display custom QLabel
Quote:
Originally Posted by
spawnwj
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?
Re: QScrollArea display custom QLabel
Quote:
Originally Posted by
jpn
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
Re: QScrollArea display custom QLabel
Quote:
Originally Posted by
spawnwj
But how do I pass the event to the base class implementation?
Code:
{
// let QLabel implementation paint the image
// do some custom drawing here...
}
Re: QScrollArea display custom QLabel
Quote:
Originally Posted by
jpn
Code:
{
// let QLabel implementation paint the image
// 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();
}
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.
Re: QScrollArea display custom QLabel
Quote:
Originally Posted by
jpn
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.