Results 1 to 2 of 2

Thread: Qt window resize without infinite loop

  1. #1
    Join Date
    Apr 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Qt window resize without infinite loop

    I need to resize the window only with a 1:1 aspect ratio. Tried to implement in different ways, but it turns out recursion.
    The last thing I stopped at:

    widget.cpp:
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include "QResizeEvent"
    4. #include "filterobject.h"
    5.  
    6. Widget::Widget(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Widget)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. Widget::~Widget()
    14. {
    15. delete ui;
    16. }
    17.  
    18. bool FilterObject::eventFilter(QObject *watched, QEvent *event) {
    19. if(watched != target){//checks for correct target object.
    20. return false;
    21. }
    22.  
    23. if(event->type() != QEvent::Resize){//and correct event
    24. return false;
    25. }
    26. QResizeEvent *resEvent = static_cast<QResizeEvent*>(event);//then sets correct event type
    27.  
    28. goalHeight = 7*resEvent->size().width()/16;//calculates height, 7/16 of width in my case
    29. if(target->height()!=goalHeight){
    30. target->setFixedHeight(goalHeight);
    31. }
    32.  
    33. return true;
    34. };
    35.  
    36. void QWidget::resizeEvent(QResizeEvent *event){
    37. FilterObject *filter = new FilterObject();
    38. QWidget *targetWidget = new QWidget();//let it be target object
    39. filter->target=targetWidget;
    40. targetWidget->installEventFilter(filter);
    41. }
    To copy to clipboard, switch view to plain text mode 
    filterobject.h:

    Qt Code:
    1. #include <QWidget>
    2.  
    3. class FilterObject:public QObject{
    4. public:
    5. QWidget *target = nullptr;//it holds a pointer to target object
    6. int goalHeight=0;
    7. FilterObject(QObject *parent=nullptr):QObject(parent){}//uses QObject constructor
    8. bool eventFilter(QObject *watched, QEvent *event) override;//and overrides eventFilter function
    9. };
    To copy to clipboard, switch view to plain text mode 
    I think that
    Qt Code:
    1. if (event-> type ()! = QEvent :: Resize)
    To copy to clipboard, switch view to plain text mode 
    code does not work, because the function does not continue.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt window resize without infinite loop

    Some things look very weird:

    * why create and install an even filter in every resize event?
    * currently the event filter discards all resize events but it needs to let the correct ones pass

    Cheers,
    _

Similar Threads

  1. QThread : how to stop an infinite loop
    By TarielVincent in forum Qt Programming
    Replies: 9
    Last Post: 24th February 2012, 22:22
  2. Infinite loop in QXmlSchemaValidator::validate()?
    By TropicalPenguin in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2010, 16:09
  3. infinite loop
    By zakis in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2009, 18:52
  4. Infinite loop - resize parent from child
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2006, 14:21
  5. is it possible to stay on a user defined infinite loop?
    By mahe2310 in forum Qt Programming
    Replies: 9
    Last Post: 24th March 2006, 15:29

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.