C167
22nd June 2008, 00:05
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
class ImageLabel : public QWidget
{
Q_OBJECT
public:
ImageLabel ( QWidget *parent = 0 );
signals:
void renderingFinished ();
public slots:
void setImage ( const int&, const QByteArray& );
protected:
void resizeEvent ( QResizeEvent* );
private:
void resize ( QSize );
QHBoxLayout *layout;
QLabel *imageLabel;
QPixmap image;
QSize imageSize;
};ImageLabel.cpp
ImageLabel::ImageLabel ( QWidget *parent )
: QWidget ( parent )
{
layout = new QHBoxLayout ( this );
imageLabel = new QLabel ( this );
layout->addWidget ( imageLabel );
}
void ImageLabel::setImage ( const int &handle, const QByteArray &data )
{
Q_UNUSED ( handle );
QImage img = QImage::fromData ( data );
// imageLabel->setPixmap ( QPixmap() );
if ( !img.isNull() )
{
image = QPixmap::fromImage ( img );
imageLabel->setPixmap ( image );
resize ( size() );
}
else
{
imageLabel->setText("No Image");
}
}
void ImageLabel::resizeEvent ( QResizeEvent *event )
{
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:
imageLabel = new ImageLabel ( this );
imageLabel->setObjectName ( QString::fromUtf8 ( "imageLabel" ) );
imageLabel->setSizePolicy ( QSizePolicy::Ignored, QSizePolicy::Ignored );
imageLabel->setBackgroundRole ( QPalette::Base );
imageLabel->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
vBox->addWidget ( imageLabel );and for the crazy one:
screen = new ImageLabel ( this );
screen->setObjectName ( QString::fromUtf8 ( "screen" ) );
screen->setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
screen->setBackgroundRole ( QPalette::Base );
screen->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
//load Image
void BinaryViewer::showImages ( )
{
QSqlQuery query ( QSqlDatabase::database() );
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
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
class ImageLabel : public QWidget
{
Q_OBJECT
public:
ImageLabel ( QWidget *parent = 0 );
signals:
void renderingFinished ();
public slots:
void setImage ( const int&, const QByteArray& );
protected:
void resizeEvent ( QResizeEvent* );
private:
void resize ( QSize );
QHBoxLayout *layout;
QLabel *imageLabel;
QPixmap image;
QSize imageSize;
};ImageLabel.cpp
ImageLabel::ImageLabel ( QWidget *parent )
: QWidget ( parent )
{
layout = new QHBoxLayout ( this );
imageLabel = new QLabel ( this );
layout->addWidget ( imageLabel );
}
void ImageLabel::setImage ( const int &handle, const QByteArray &data )
{
Q_UNUSED ( handle );
QImage img = QImage::fromData ( data );
// imageLabel->setPixmap ( QPixmap() );
if ( !img.isNull() )
{
image = QPixmap::fromImage ( img );
imageLabel->setPixmap ( image );
resize ( size() );
}
else
{
imageLabel->setText("No Image");
}
}
void ImageLabel::resizeEvent ( QResizeEvent *event )
{
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:
imageLabel = new ImageLabel ( this );
imageLabel->setObjectName ( QString::fromUtf8 ( "imageLabel" ) );
imageLabel->setSizePolicy ( QSizePolicy::Ignored, QSizePolicy::Ignored );
imageLabel->setBackgroundRole ( QPalette::Base );
imageLabel->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
vBox->addWidget ( imageLabel );and for the crazy one:
screen = new ImageLabel ( this );
screen->setObjectName ( QString::fromUtf8 ( "screen" ) );
screen->setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
screen->setBackgroundRole ( QPalette::Base );
screen->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
//load Image
void BinaryViewer::showImages ( )
{
QSqlQuery query ( QSqlDatabase::database() );
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