Resize window using QPropertyAnimation - qt

I have the following problem: I want to resize a window using QPropertyAnimation, so as to look nice, but the window also instantly moves while the resizing takes place, while I don't want it to, I just want to resize the window and not to alter its position value.
So, here's my code:
animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(150);
animation->setStartValue(QRect(preferences::x(), preferences::y(), width, window_height_min));
animation->setEndValue(QRect(preferences::x(), preferences::y(), width, window_height_min+expand_general_to));
animation->start();
where width and window_height_min and expand_general_to are my own variables that handle the amount of the resizing that has to be done. BUT, preferences::x() and preferences::y() truly handle the position of my window, so why it moves, while prefereces::x() will be the same both of the times? (in the start and in the end value) ?
Thanks in advance for any answers!

Please try to set size property.
animation = new QPropertyAnimation(this, "size");
animation->setDuration(150);
animation->setStartValue(QSize(width, window_height_min));
animation->setEndValue(QSize(width, window_height_min+expand_general_to));
animation->start();

Related

Automatically resizing a window based on its contents

I have a QDialog subclass that contains a spacer as its only immediate child; all of the window’s UI elements are contained in the spacer. The user cannot change the window size directly, but UI elements will be shown or hidden as the user interacts with the window. I’d like the dialog to resize each time this happens so that the spacer (and the dialog itself) always takes up the minimum possible amount of space. How can I configure my dialog and my spacer to get the desired behavior?
(This question dealt with something similar, although in that case the user was able to resize the window. It was also not clear to me what the OP actually ended up doing in that case.)
You can resize the window to minimumSizeHint() after the number of widgets is changed :
resize(minimumSizeHint());
This will shrink the window to minimum size. But you should consider that the minimum size is not computed until some events are processed in the event loop. So after some widgets are hidden and some other are shown, just process the event loop for some iterations and then resize to minimum.
It's like :
for(int i=0;i<10;i++)
qApp->processEvents();
resize(minimumSizeHint());
A better solution is to single shot a QTimer which calls a slot in which you resize the window to minimum. This way when you resize the window, the minimum size hint is computed correctly.
void QWidget::adjustSize()
Adjusts the size of the widget to fit its contents.

QMainWindow::showMaximized() Doesn't Update Size

I'm trying to create a QMainWindow to encapsulate a QGraphicsView that I'm putting in it. I want it to start out maximized, so I do this:
QMainWindow *mainWindow = new QMainWindow();
mainWindow->setWindowState(Qt::WindowMaximized);
mainWindow->show();
qDebug() << mainWindow->size();
Which says my maximized window is 200x100, which is obviously incorrect.
Am I missing some sort of update function? I don't get why it wouldn't update the size. I've also tried using showMaximized() with the same result.
EDIT
My end-goal is to use the QMainWindow as a container for a QGraphicsView containing a QGraphicsScene. On top of all this, I want to have a QWebView at 50% width and 100% height, centered over everything.
So, I need the width and height in order to get all the coordinates and sizes correct.
Well, the effect of setWindowState() is not immediate, it gets executed asynchronously. When the window state changes, the widget receives a changeEvent(), so you should reimplement this or resizeEvent() to get the width() and height() after the maximization takes place.

Resize window to fit content

I have a QGLWidget, which I want to resize to a given resolution (bigger than the containing window).
My intention is, that the window expands until the widget fits inside, but can't find a way to do it.
I tried several commands after resizing the QGLWidget to make it work without success.
I will list the results here:
do nothing else: The Widget overlaps the whole window. Eventually it will be resized to fit back into the smaller window
mainWindow.adjustSize(): The widget gets resized to (0, 0)
mainWindow.resize(mainWindow.sizeHint()): see above
mainWindow.resize(mainWindow.minimumSizeHint()): see above
I also read in this thread, that before doing the mainWindow resize I the event loop needs to be run to recalculate the new sizes, so I inserted QCoreApplication::processEvents to do so, without any visible effect.
So how do I resize the window via the widget?
Edit
The GLWidget is not the only widget of the window.
It is embedded in splitter together with a group box.
http://qt-project.org/doc/qt-4.8/qwidget.html#sizePolicy-prop
http://qt-project.org/doc/qt-4.8/qsizepolicy.html#Policy-enum
http://qt-project.org/doc/qt-4.8/qwidget.html#setFixedSize
So assuming that you have your QGLWidget nested inside your QMainWindow as the central widget, you need to set the size policy of your QGLWidget.
For example:
QGLWidget * glw; // in your header for QMainWindow
...
// In your constructor for QMainWindow
glw = new QGLWidget;
this->setCentralWidget(glw);
glw->setFixedSize(500, 500);
this->adjustSize();
Hope that helps.
I have an app that needed to be very similar to your requirements, so I'll post my solution here. An image covering the window which is freely expandable and shrinkable, and can be changed to the original size, and remain expandable / shrinkable after that.
I used a QLabel widget to display the image, but it should work with other widget types too. I created the widget with an initial size and the QSizePolicy::Ignored.
label->resize (w, h); // initial size
label->setSizePolicy (QSizePolicy::Ignored, QSizePolicy::Ignored);
The label widget was in a QVBoxLayout with a few buttons in the window, but this may work with other layout types too.
The window and image widget can be resized to the image's original size with this code:
label->resize (w, h); // change to original size
label->setMinimumSize (w, h); // prevent it from collapsing to zero immediately
window->adjustSize (); // resize the window
label->setMinimumSize (0, 0); // allow shrinking afterwards

I want qt window to resize proportional

Just as the title said, when I resize the window, if I make it widen , I want the window's height resize proportional to make the window has the same ratio.
I just do the logic in paintEvent, but it doesn't work fine, can some guys have any solution?
I don't think do resizing in resizeEvent is a great way, the best result is that window flickering, but I want the fluent way
Don't do it in paintEvent but instead void QWidget::resizeEvent ( QResizeEvent * event ). Then you can compare event->size() and event->oldsize() to see which dimension got changed, then resize to match the other dimension to the changed one.
EDIT: Note that when you resize the window inside the resizeEvent function, you will create another resize event. So make sure you only resize if there's been a change that brought the window to a wrong aspect ratio, otherwise you will create infinite recursion to the function and a stack overflow.

Resize Flex/Flash object from the left side?

I'm working on Flex component which can be resized using handles on the right and left (that is, if you click and drag the left side, the component will grow to the left, if you click and drag on the right, it will grow to the right).
Right now I am using:
var oldX:Number = this.x;
this.x = event.stageX + (initial.x - initial.stageX); // Move the left edge to the left
this.width += oldX - this.x; // Increase the width to compensate for the move to the left
But that makes the right side jump around, and generally looks ugly.
What is the "right" to do this? Is there something fundamental I've gotten wrong?
Thanks!
Edit: The jitter occurs on the right side of the component. When I set this.x, the component moves to the left, the screen redraws, then the width is updated, and the screen redraws again.
I'm not a Flex guy, but I imagine your jitter is something to do with the Flex framework internally using Stage.invalidate() or some such thing to cause redraws during frame execution.
I imagine there's probably a "framework" way to deal with the problem, but for a workaround to make your change into one atomic operation you could update the object's transform directly, along these lines:
var dx:Number = // however you're finding this
var m:Matrix = new Matrix();
m.scale( scaleX*(width-dx)/width, scaleY );
m.translate( x+dx, y );
transform.matrix = m;
That should work for a simple graphic, but I'm not sure if it would work with a component that probably needs to catch events that the dimensions have changed and redraw itself. I think that's the only way to make your update atomic though, so if something along those lines doesn't help then you'll need an answer from someone more up on the framework.
Had the same problem.
The problem is that the stage isn't refreshing enough.
Just add stage.invalidate(); after your 2 assignments ...
Depending on how you have the listeners for the handles set up, the problem could be that the resize code is called over and over again because when you set the new x position for the component the handle will go under the mouse cursor effectively calling the resize code again and so on.

Resources