PDA

View Full Version : Resize QMainWindow without layout on it



shock
11th December 2011, 11:46
Hey,

I have troubles on replacing items on resizeEvent of the QMainWindow - I am not using any layout, i just recalculate the new positions on resize.
If the window is resized slowly there are not any troubles but if i resize it very fast by moving the mouse in crazy ways i get weird results....
I added a "before resize" and a "after resize" screenshot to illustrate the problem.

The Code looks like follows:


void ClientMainWindow::resizeEvent(QResizeEvent * event){
int heightPositionCorrection = (event->size().height()-event->oldSize().height())/2;
int widthPositionCorrection = (event->size().width()-event->oldSize().width())/2;

ui.controlAirCondition->setGeometry(
ui.controlAirCondition->geometry().x()+widthPositionCorrection,
ui.controlAirCondition->geometry().y()+heightPositionCorrection,
ui.controlAirCondition->geometry().width(),ui.controlAirCondition->geometry().height());
ui.controlAlarmSystem->setGeometry(
ui.controlAlarmSystem->geometry().x()+widthPositionCorrection,
ui.controlAlarmSystem->geometry().y()+heightPositionCorrection,
ui.controlAlarmSystem->geometry().width(),ui.controlAlarmSystem->geometry().height());
ui.controlBlinds->setGeometry(
ui.controlBlinds->geometry().x()+widthPositionCorrection,
ui.controlBlinds->geometry().y()+heightPositionCorrection,
ui.controlBlinds->geometry().width(),ui.controlBlinds->geometry().height());
ui.controlHeating->setGeometry(
ui.controlHeating->geometry().x()+widthPositionCorrection,
ui.controlHeating->geometry().y()+heightPositionCorrection,
ui.controlHeating->geometry().width(),ui.controlHeating->geometry().height());
ui.controlLights->setGeometry(
ui.controlLights->geometry().x()+widthPositionCorrection,
ui.controlLights->geometry().y()+heightPositionCorrection,
ui.controlLights->geometry().width(),ui.controlLights->geometry().height());
ui.controlMusic->setGeometry(
ui.controlMusic->geometry().x()+widthPositionCorrection,
ui.controlMusic->geometry().y()+heightPositionCorrection,
ui.controlMusic->geometry().width(),ui.controlMusic->geometry().height());
ui.controlVideo->setGeometry(
ui.controlVideo->geometry().x()+widthPositionCorrection,
ui.controlVideo->geometry().y()+heightPositionCorrection,
ui.controlVideo->geometry().width(),ui.controlVideo->geometry().height());
ui.configScreen->setGeometry(
ui.configScreen->geometry().x()+widthPositionCorrection,
ui.configScreen->geometry().y()+heightPositionCorrection,
ui.configScreen->geometry().width(),ui.configScreen->geometry().height());
ui.controlPlaceholder->setGeometry(
ui.controlPlaceholder->geometry().x()+widthPositionCorrection,
ui.controlPlaceholder->geometry().y()+heightPositionCorrection,
ui.controlPlaceholder->geometry().width(),ui.controlPlaceholder->geometry().height());
}
71657166

What am i doing wrong here?

ChrisW67
12th December 2011, 02:10
I cannot get this to fail on Linux, though I assume from the screenshot that you are on Windows.

Exactly what manipulation causes the effect you see? Does Windows allow you to drag beyond screen edge during resize (taking the icons with it)? Does the window then get constrained to the screen without a resize event bringing the icons back into the smaller size perhaps? Does the behaviour change if you call the QMainWindow::resizeEvent() at the end or your resize event?

A couple of observations:

Since you are only moving the icons, not resizing them, you can use QWidget::move() and save yourself some typing.
In a similar vein, you don't need to go via geometry() to get the QWidget::x() and QWidget::y()

shock
12th December 2011, 19:56
Yeah, it's correct I am under windows (XP 32Bit, SP3)
The manipulation I am using is the normal resize my clicking on the bottom right of the window and then doing the resize operation..
I changed the code a bit to fit your ideas, but it did not fix the problem =/


void ClientMainWindow::resizeEvent(QResizeEvent * event){
if(/*resizing == false && */(event->size().height() > 500 || event->size().width() > 800)){
//resizing = true;
int heightPositionCorrection = (event->size().height()-event->oldSize().height())/2;
int widthPositionCorrection = (event->size().width()-event->oldSize().width())/2;

ui.controlAirCondition->move(
ui.controlAirCondition->x()+widthPositionCorrection,
ui.controlAirCondition->y()+heightPositionCorrection);
ui.controlAlarmSystem->move(
ui.controlAlarmSystem->x()+widthPositionCorrection,
ui.controlAlarmSystem->y()+heightPositionCorrection);
ui.controlBlinds->move(
ui.controlBlinds->x()+widthPositionCorrection,
ui.controlBlinds->y()+heightPositionCorrection);
ui.controlHeating->move(
ui.controlHeating->x()+widthPositionCorrection,
ui.controlHeating->y()+heightPositionCorrection);
ui.controlLights->move(
ui.controlLights->x()+widthPositionCorrection,
ui.controlLights->y()+heightPositionCorrection);
ui.controlMusic->move(
ui.controlMusic->x()+widthPositionCorrection,
ui.controlMusic->y()+heightPositionCorrection);
ui.controlVideo->move(
ui.controlVideo->x()+widthPositionCorrection,
ui.controlVideo->y()+heightPositionCorrection);
ui.configScreen->move(
ui.configScreen->x()+widthPositionCorrection,
ui.configScreen->y()+heightPositionCorrection);
ui.controlPlaceholder->move(
ui.controlPlaceholder->x()+widthPositionCorrection,
ui.controlPlaceholder->y()+heightPositionCorrection);
QMainWindow::resizeEvent(event);
//resizing = false;
}
}

I added 3 Screenshots again - the behaviour is the same as before =(

The one to the left is before resizing, the second from the left side is after "slow" resizing and the third one is after fast resizing (I am moving the mouse as fast as I can while I am holding the left mousebutton ... I move it in kinda circles very fast again and again before I release the left mouse button and this is what I get)

717171727173

From my point of view it seems like the "resizeEvent" is called too often, so that one resizeEvent breaks up the running one.. (like one event is NOT fully processed while the next already happend..) -> is that possible!?

Lesiok
13th December 2011, 17:40
Quote from the QWidget::move method documentation : When changing the position, the widget, if visible, receives a move event (moveEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive an event before it is shown. Maybe this is a problem.

shock
18th December 2011, 09:40
Hmm, ok - and if that's the problem ... how should i handle that? Any ideas?

shock
19th December 2011, 21:05
So does not anybody have an idea how to solve this? I think I got your point @Lesiok - but how do I solve that issue!?

marcvanriet
19th December 2011, 23:33
Why do you use a movement based on the old and new window sizes ?

Can't you just calculate a new position based on the new size ? So always calculate an absolute position, not a relative position compared to the old position. That would be much less error-prone.

Regards,
Marc