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:
#include "widget.h"
#include "ui_widget.h"
#include "QResizeEvent"
#include "filterobject.h"
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
bool FilterObject
::eventFilter(QObject *watched,
QEvent *event
) { if(watched != target){//checks for correct target object.
return false;
}
if(event
->type
() != QEvent::Resize){//and correct event return false;
}
QResizeEvent *resEvent
= static_cast<QResizeEvent
*>
(event
);
//then sets correct event type
goalHeight = 7*resEvent->size().width()/16;//calculates height, 7/16 of width in my case
if(target->height()!=goalHeight){
target->setFixedHeight(goalHeight);
}
return true;
};
FilterObject *filter = new FilterObject();
filter->target=targetWidget;
targetWidget->installEventFilter(filter);
}
#include "widget.h"
#include "ui_widget.h"
#include "QResizeEvent"
#include "filterobject.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
bool FilterObject::eventFilter(QObject *watched, QEvent *event) {
if(watched != target){//checks for correct target object.
return false;
}
if(event->type() != QEvent::Resize){//and correct event
return false;
}
QResizeEvent *resEvent = static_cast<QResizeEvent*>(event);//then sets correct event type
goalHeight = 7*resEvent->size().width()/16;//calculates height, 7/16 of width in my case
if(target->height()!=goalHeight){
target->setFixedHeight(goalHeight);
}
return true;
};
void QWidget::resizeEvent(QResizeEvent *event){
FilterObject *filter = new FilterObject();
QWidget *targetWidget = new QWidget();//let it be target object
filter->target=targetWidget;
targetWidget->installEventFilter(filter);
}
To copy to clipboard, switch view to plain text mode
filterobject.h:
#include <QWidget>
public:
QWidget *target
= nullptr;
//it holds a pointer to target object int goalHeight=0;
FilterObject
(QObject *parent
=nullptr
):QObject(parent
){}//uses QObject constructor bool eventFilter
(QObject *watched,
QEvent *event
) override;
//and overrides eventFilter function };
#include <QWidget>
class FilterObject:public QObject{
public:
QWidget *target = nullptr;//it holds a pointer to target object
int goalHeight=0;
FilterObject(QObject *parent=nullptr):QObject(parent){}//uses QObject constructor
bool eventFilter(QObject *watched, QEvent *event) override;//and overrides eventFilter function
};
To copy to clipboard, switch view to plain text mode
I think that
if (event
-> type
()! = QEvent :: Resize)
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.
Bookmarks