Results 1 to 14 of 14

Thread: ResizeEvent/Resize out of control

  1. #1
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. class ImageLabel : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. ImageLabel ( QWidget *parent = 0 );
    6. signals:
    7. void renderingFinished ();
    8. public slots:
    9. void setImage ( const int&, const QByteArray& );
    10. protected:
    11. void resizeEvent ( QResizeEvent* );
    12. private:
    13. void resize ( QSize );
    14. QHBoxLayout *layout;
    15. QLabel *imageLabel;
    16. QPixmap image;
    17. QSize imageSize;
    18. };
    To copy to clipboard, switch view to plain text mode 
    ImageLabel.cpp
    Qt Code:
    1. ImageLabel::ImageLabel ( QWidget *parent )
    2. : QWidget ( parent )
    3. {
    4. layout = new QHBoxLayout ( this );
    5. imageLabel = new QLabel ( this );
    6. layout->addWidget ( imageLabel );
    7. }
    8.  
    9. void ImageLabel::setImage ( const int &handle, const QByteArray &data )
    10. {
    11. Q_UNUSED ( handle );
    12. QImage img = QImage::fromData ( data );
    13. // imageLabel->setPixmap ( QPixmap() );
    14. if ( !img.isNull() )
    15. {
    16. image = QPixmap::fromImage ( img );
    17. imageLabel->setPixmap ( image );
    18. resize ( size() );
    19. }
    20. else
    21. {
    22. imageLabel->setText("No Image");
    23. }
    24. }
    25.  
    26. void ImageLabel::resizeEvent ( QResizeEvent *event )
    27. {
    28. qDebug() << "resize event: " << event->size();
    29. imageSize = event->size();
    30. resize ( event->size() );
    31. }
    32.  
    33. void ImageLabel::resize ( QSize size )
    34. {
    35. if ( !image.isNull() )
    36. {
    37. imageLabel->setPixmap ( image.scaled ( size.width()-8, size.height()-8, Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. imageLabel = new ImageLabel ( this );
    2. imageLabel->setObjectName ( QString::fromUtf8 ( "imageLabel" ) );
    3. imageLabel->setSizePolicy ( QSizePolicy::Ignored, QSizePolicy::Ignored );
    4. imageLabel->setBackgroundRole ( QPalette::Base );
    5. imageLabel->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
    6. vBox->addWidget ( imageLabel );
    To copy to clipboard, switch view to plain text mode 
    and for the crazy one:
    Qt Code:
    1. screen = new ImageLabel ( this );
    2. screen->setObjectName ( QString::fromUtf8 ( "screen" ) );
    3. screen->setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
    4. screen->setBackgroundRole ( QPalette::Base );
    5. screen->setStyleSheet ( "border-width: 1px; border-style: solid; border-color: black;" );
    6.  
    7. //load Image
    8. void BinaryViewer::showImages ( )
    9. {
    10. QSqlQuery query ( QSqlDatabase::database() );
    11. query.prepare ( QString ( "SELECT img0 FROM bin_programs WHERE id = :id" ) );
    12. query.bindValue ( ":id", id );
    13. query.exec();
    14. query.next();
    15. QByteArray array = query.value ( 0 ).toByteArray();
    16. screen->setImage ( 0, array );
    17. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?

  3. #3
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: ResizeEvent/Resize out of control

    You can start from this:

    Qt Code:
    1. class ScalableImage : public QWidget {
    2. public:
    3. ScalableImage(..) ... { ... m_wdiff = 0; m_hdiff = 0; }
    4. void setPixmap(const QPixmap &px){
    5. m_px = px;
    6. recalculate();
    7. updateGeometry();
    8. update();
    9. }
    10. QSize sizeHint() const { return m_px.isNull() ? QSize(100,100) : m_px.size(); }
    11. protected:
    12. void resizeEvent(QResizeEvent *ev){
    13. if(m_px.isNull()) return;
    14. recalculate();
    15. update();
    16. }
    17. void paintEvent(QPaintEvent *pe){
    18. if(m_px.isNull()) return;
    19. QPainter painter(this);
    20. painter.drawPixmap(m_wdiff/2, m_hdiff/2, m_scaled);
    21. }
    22. void recalculate(){
    23. m_scaled = m_px.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    24. m_wdiff = width()-m_scaled.width();
    25. m_hdiff = height()-m_scaled.height();
    26. }
    27. private:
    28. QPixmap m_px, m_scaled;
    29. int m_wdiff, m_hdiff;
    30. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  6. #6
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  8. #8
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

    Qt Code:
    1. void xxx:showEvent(QShowEvent *e){
    2. recalculate();
    3. update();
    4. QWidget::showEvent(e);
    5. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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 :-)

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: ResizeEvent/Resize out of control

    You probably forgot to apply some of the layouts.

  12. #12
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. screen1->setSizePolicy ( QSizePolicy::Ignored, QSizePolicy::Ignored );
    2. screen2->setSizePolicy ( QSizePolicy::Ignored, QSizePolicy::Ignored );
    To copy to clipboard, switch view to plain text mode 
    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).

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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

  14. #14
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

Similar Threads

  1. Replies: 3
    Last Post: 7th October 2015, 19:43
  2. Text shifts when control appears...
    By MrGarbage in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2008, 03:12
  3. Version control - what to use?
    By TheKedge in forum General Programming
    Replies: 11
    Last Post: 4th March 2007, 08:49
  4. QTextEdit control
    By vijay anandh in forum Qt Programming
    Replies: 2
    Last Post: 31st May 2006, 13:14
  5. Replies: 6
    Last Post: 3rd February 2006, 09:48

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.