ResizeEvent/Resize out of control
Hi, another problem :-)
I have a class ImageLabel that receives a QByteArray and converts it into an image. To be able to control resizing, i re-implemented the resizeEvent of the widget. I use it in two positions of the code, both time its in some H/VBox layouts. One time, it works as expected: It loads an image and resizes it as it should if the window gets resized.
On the other position it runs crazy. Instead of scaling the image once to the maximum size, it resizes to infinity! It slowly resizes, and finallyresizes the whole window. I have absolutly no idea why this happens. First, some code:imageLabel.h
Code:
{
Q_OBJECT
public:
ImageLabel
( QWidget *parent
= 0 );
signals:
void renderingFinished ();
public slots:
void setImage ( const int&, const QByteArray& );
protected:
private:
};
ImageLabel.cpp
Code:
ImageLabel
::ImageLabel ( QWidget *parent
){
imageLabel
= new QLabel ( this );
layout->addWidget ( imageLabel );
}
void ImageLabel
::setImage ( const int &handle,
const QByteArray &data
) {
Q_UNUSED ( handle );
// imageLabel->setPixmap ( QPixmap() );
if ( !img.isNull() )
{
image
= QPixmap::fromImage ( img
);
imageLabel->setPixmap ( image );
resize ( size() );
}
else
{
imageLabel->setText("No Image");
}
}
{
qDebug() << "resize event: " << event->size();
imageSize = event->size();
resize ( event->size() );
}
void ImageLabel
::resize ( QSize size
) {
if ( !image.isNull() )
{
imageLabel->setPixmap ( image.scaled ( size.width()-8, size.height()-8, Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
}
}
The only differences between the two position is that one receives the QByteArray through a QueuedConnection and the other (the crazy one) through a normal method call. My code to create the label is for the working part:
Code:
imageLabel = new ImageLabel ( this );
imageLabel
->setObjectName
( QString::fromUtf8 ( "imageLabel" ) );
imageLabel
->setBackgroundRole
( QPalette::Base );
imageLabel->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
vBox->addWidget ( imageLabel );
and for the crazy one:
Code:
screen = new ImageLabel ( this );
screen
->setObjectName
( QString::fromUtf8 ( "screen" ) );
screen
->setBackgroundRole
( QPalette::Base );
screen->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
//load Image
void BinaryViewer::showImages ( )
{
query.
prepare ( QString ( "SELECT img0 FROM bin_programs WHERE id = :id" ) );
query.bindValue ( ":id", id );
query.exec();
query.next();
QByteArray array
= query.
value ( 0 ).
toByteArray();
screen->setImage ( 0, array );
}
I played around with the size policys, but with Ignored, i get no object at all (does not allocate space in the gui etc.)
This problem first occured on windows, and after some changes (which i try to recover from the svn currently) it also happens on linux! I'm now in big troubles, cause it destroys my timetable and the program should be finished by monday morning :-(
Any hints?
C167
Re: ResizeEvent/Resize out of control
There is a method in QWidget called resize() and it is not virtual, thus your version of resize() probably never gets called and you get an infinite loop of resize-resizeEvent calls. Have you tried simply setting scaledContents of QLabel to true and using a plain label instead?
Re: ResizeEvent/Resize out of control
scaledContents is what i wanted to avoid, cause it destroys the proportions of the image. I renamed the method to resizeImage, but still no change.
Re: ResizeEvent/Resize out of control
You can start from this:
Code:
class ScalableImage
: public QWidget {public:
ScalableImage(..) ... { ... m_wdiff = 0; m_hdiff = 0; }
m_px = px;
recalculate();
updateGeometry();
update();
}
QSize sizeHint
() const { return m_px.
isNull() ?
QSize(100,
100) : m_px.
size();
} protected:
if(m_px.isNull()) return;
recalculate();
update();
}
if(m_px.isNull()) return;
painter.drawPixmap(m_wdiff/2, m_hdiff/2, m_scaled);
}
void recalculate(){
m_scaled = m_px.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
m_wdiff = width()-m_scaled.width();
m_hdiff = height()-m_scaled.height();
}
private:
int m_wdiff, m_hdiff;
};
Re: ResizeEvent/Resize out of control
Oh, wow, QPainter for painting instead of that QLabel! And now the images are centered instead of left aligned! Thank you very much
I still haven't figured out why it resized, but okay, code is in svn, i won't loose any information :)
Thanks
C167
Re: ResizeEvent/Resize out of control
oh, i've run in a little problem: the images seem to only show up after resizing the whole window. i tried to manually resize it (same code like in paintEvent) but that doesn't work
Re: ResizeEvent/Resize out of control
Yes, of course, my bad. If you call setPixmap() before show() is called, recalculate() will calculate the new pixmap based on incorrect width and height of the widget (although it's a bit strange as the showEvent should trigger a resizeEvent). You can reimplement showEvent() and call recalculate() and update() there and everything should be fine. The widget will repaint itself twice, but that's a minor problem as one of the runs will return immediately. If you want to fix it, you should for example assign m_px to m_scaled in setPixmap and set the margins to 0 instead of calling recalculate(). If the size hint of the widget is kept, the image will be fine.
Re: ResizeEvent/Resize out of control
At the moment, i have no idea what update does ;) Qt tells me:
Quote:
Originally Posted by Qt 4.4
Widget painting can only begin as a result of a paintEvent
Thats cause i just used the same code in update as for paintEvent
Re: ResizeEvent/Resize out of control
You don't implement update, you call it and Qt schedules a paintEvent. You were to implement showEvent() and call recalculate() and update() there.
Code:
recalculate();
update();
}
Re: ResizeEvent/Resize out of control
i'm sorry, i just found functions that expected some parameters, is missed the normal QWidget::update() :-)
In one place where i have two instanced in a tab widget as individual tabs, there is now the problem that after the images load, the widgets resize to nearly the full size of the image rather than to the size of the widget. But except that, it works. Thank you :-)
Re: ResizeEvent/Resize out of control
You probably forgot to apply some of the layouts.
Re: ResizeEvent/Resize out of control
not directly layouts ;) I felt the power of QSizePolicy:
not the tab widget itself, but the objects that show the images need a policy:hehe, again, thank you very much. As everything works well (and now, the images show up immediatly in the tabWidget instead of needing 7 seconds to load without sizePolicys), i'm very happy that the program is finally finished (my biggest program ever).
Re: ResizeEvent/Resize out of control
No, certainly Ignored is not needed. If you think you need it, you must have done something Bad. I've been programming Qt for more than 4 years and I never needed to set the size policy to ignored :)
Re: ResizeEvent/Resize out of control
Hm... then i may be on the wrong path in general when making GUIs.
If you have some time, could you please have a look at what i did to make the gui? The files are here:
Base to the vcs.
The GUI-Files are BinaryViewer.cpp, BinaryViewer.h, ImageViewer.cpp, ImageViewer.h. There are some others, but they have no real GUI but provide a simple Stack that shows the actual GUIs.
Thanks, C167