PDA

View Full Version : Problem dealing with resizeEvent



Momergil
14th January 2013, 18:57
Hello!

I'm using Qwt to draw a graph and doing it by code I need to uset setParent() function to a QWidget inside my form. But if I simply do that, QwtPlot appears very small; don't acquire the QWidget's size.
To solve this, I inserted the line
graphplot->resize(ui->graphWidget->size()) just after the creation of QwtPlot object and it seems everything was fine. But then I noticed that each time my app's window was resized (e.g. from normal size to Maximized, etc.), while the widget was resized, the QwtPlot remained in the original size after that line of code I wrote.
So I went to mainwindow->resizeEvent(QResizeEvent *) and add the line
graphPlot->resize(ui->graphWidget->size());; immediately this last problem was solved, but now a new one appear: each time the software is opened, the QwtPlot is not ajusted to its parent's size: it's as if the two parts of the code were somehow collapsing.

Any ideas about how to fix this?


Thanks,

Momergil

raynerd
14th January 2013, 21:05
If graphPlot inherits from any kind of widget you can use a QLayout to resize it automatically.

Momergil
15th January 2013, 01:37
If graphPlot inherits from any kind of widget you can use a QLayout to resize it automatically.

Thanks raynerd, but I actually have no idea of how that would go.

Besides, I found out that I was mistaken regarding the reason of my problem; actually is nothing to do with resizeEvent(), but rather a probable bug with Qt's layout sistem (at least using the form) and the size of a given widget.

In case, when I put the QWidget in the ui I put so it's width and height would use all of the avaliable space there inside a QTabWidget (with some buttons working as menu). When that was done pure naturally, there was no problem and everything run fine but I couldn't use the resizeEvent() because the QWidget for my graph wouldn't change form when the window was maximized or changed size another way. So to counter this I put the "menu" and the graph's QWidget inside a QGridLayout, and while now the resizeEvent() scheme would work, for some reason when the software was opened the size of the QWidget would come different - and wrong.

Since I needed the layout in order for the resizeEvent() to work, what I did was to use the QGridLayout and just prior the setParent() of Qwt's QwtPlot item to the QWidget I did a forced resize() in the QWidget with the size it should have. That's certanly not the most elegant way of doing it, but it was the solution I found. I just hope Qt's guys will notice that and fix the bug.



Thanks,

Momergil

ChrisW67
15th January 2013, 02:10
... but rather a probable bug with Qt's layout sistem (at least using the form) and the size of a given widget.
It would be a very improbable bug indeed.

You are misusing the layout system as evidenced by:

what I did was to use the QGridLayout and just prior the setParent() of Qwt's QwtPlot item to the QWidget I did a forced resize() in the QWidget with the size it should have.
You add the child widget to the layout (set on the parent widget) which is entirely responsible for the size of the widget and also sets the widget's parent. You do not resize the child widget or dictate "the size it should have"; that's the layout's job, which is does using the size constraints and policies it has. You should read Layout Management

We cannot fix your specific bug because we cannot see your code.

Momergil
17th January 2013, 01:16
Well I'm not sure I did something wrong o.O

Revizing the order:

* Go to the form: place the QWidget and the items forming the menu. Click in the QTabWidget and in "Lay out in a Grid" in the upper menu of Qt Creator's "Designer". (So now changes in the size of the widget would cause changes in the size of my QWidget)
* Go to the code:
** Reimplement Mainwindow's resizeEvent() function. There, do a QwtPlot->resize(QWidget->size()); (So now each time the mainwindow's size is changed, it will change the size of the QTabWidget which will change the size of the QWidget an than by this resizeEvent() the size of the QwtPlot will change as well).
** In class MainWindow's constructor method: First force the QWidget to the size it sould have (which is the size it appears in the form when compiling but don't appear naturally when the software is opened: here the probable bug). Then declare/instantiate QwtPlot *graphPlot = new QwtPlot(ui->QWidget). Than do a graphPlot->resize(graphPlot->parentWindow->size());

So where is the error?

wysota
17th January 2013, 04:28
Did you put that plot in the layout?

ChrisW67
17th January 2013, 04:47
So where is the error?
If the form has a layout, any container (like QTabWidget or a generic QWidget) on the form has a layout, and every widget is added to the layout of its container then everything will resize automatically: no resizeEvent() override required. If that is not happening then something is either not in a layout, missing a layout, or you have not described the problem.

I would guess you do not have a layout on the form itself (common mistake) and you have not added the Qwt plot to the layout of whatever container you intend it to be in. Cannot be sure: cannot see your UI file.

In the Designer Object Inspector look at every item in the tree with child objects. If any such item has a layout icon overlaid with a crossed red circle (like the one you see below) then it has no layout.
8589

Momergil
17th January 2013, 18:26
Did you put that plot in the layout?

Nops; the plot's parent is the QWidget, which is in a layout inside the MainWindow form.

Chris, I'll give a look when I return to development.

wysota
17th January 2013, 20:43
Nops;
If it's not in a layout then it won't auto-resize. Simple as that.