PDA

View Full Version : Moving MainWindow , Dialog Window \ Animated Move



WetCode
8th January 2013, 20:55
Hello first time poster here so i guese an intreduction is in order.
Hello am Kjell 'WetCode' Bjarre (25) and am a complete Qt noob.

So here is my question:.

What i want to do is have a dialog slide out from the top right corner of the screen "hover" for x seconds and slide back in again and close.
I set up my little main window and a button that`s suposed to trigger the "notification" in the top right corner.
Well i don`t know how i would move that image i found some functions like ::setX(int ) and Y but that don`t work, can`t find anything on it in the help. (I know its there i just cant find it).

So i tried setting up a timer that was connected the timer with the timeout() and set the slot to update() in this (beeing the dialog "pop up" window)
in a for loop that would basically move the window from X, Y = 0 to 400. That dont work ether so am hoping someone here can point me in the direction of the correct classes and functions.

Thanks for reading all this and BTW i have these express in with amazon on Thursday so i hope i wont be a bother to long. ;)
* Foundations of Qt Development (Expert's Voice in Open Source)
* C++ GUI Programming with Qt 4 (2nd Edition) (Prentice Hall Open Source Software Development Series)
* Advanced Qt Programming: Creating Great Software with C++ and Qt 4 (Prentice Hall Open Source Software Development)

I realy want to learn and can`t wait for the books so any help while i wait i is much aprichiated.

Cheers
WetCode

anda_skoa
9th January 2013, 13:41
There is no event processing as long as you are in a for loop. So when you iterate over the value range, only the final position will be drawn.

Each invocation of the slot connected to the timer needs to increase the values a bit and then exit, letting the system handle the events that this partial move has caused.

Cheers,
_

WetCode
9th January 2013, 20:09
Thank you so much for your reply.
I am allready luanching Qt Creator to have a go at modifying my code.
but where the functions correct for my intentions or do you think i should use some other methods?

Cheers
WetCode

WetCode
14th January 2013, 20:54
Forgor to put my sulution up here so bether late then never :) I hope...


void MainWindow::moveWindow()
{
thread.start();
for (int i = 0; i < 80 ; i++)
{
thread.wait( 1 );
xPoint->setX( pos().x() + 3 );
xPoint->setY( pos().y() + 3 );
move(*xPoint);
}
thread.quit();
}

Hope it helps someone and please correct me if i have done somthing "wrong" here.

Cheers
WetCode

ChrisW67
14th January 2013, 22:55
Qt has mechanisms specifically for the purpose of "animating" widgets and the like: Animation Framework, specifically QPropertyAnimation. They work entirely within the normal Qt event processing, no threads or explicit looping required, and your program can be doing actual work at the same time.



#include <QtGui>

class SelfAnimator: public QWidget
{
Q_OBJECT
public:
SelfAnimator(QWidget *p = 0): QWidget(p) {
resize(200, 200);

// Start a slide on to the screen
QPropertyAnimation *slideIn = new QPropertyAnimation(this, "pos");
slideIn->setEasingCurve(QEasingCurve(QEasingCurve::OutQuad) ); // optional, linear by default
slideIn->setDuration(5000);
slideIn->setStartValue(QPoint(-200, -200));
slideIn->setEndValue(QPoint(0, 0));
slideIn->start();
connect(slideIn, SIGNAL(finished()), SLOT(slideInFinished()));
connect(slideIn, SIGNAL(finished()), slideIn, SLOT(deleteLater()));
}

private slots:
void slideInFinished() {
// Organise to wait a while
QTimer::singleShot(3000, this, SLOT(pauseFinished()));
}

void pauseFinished() {
// Done waiting, let's leave
QPropertyAnimation *slideOut = new QPropertyAnimation(this, "pos");
slideOut->setEasingCurve(QEasingCurve(QEasingCurve::InQuad)) ;
slideOut->setDuration(5000);
slideOut->setStartValue(QPoint(0, 0));
slideOut->setEndValue(QPoint(-200, -200));
slideOut->start();
connect(slideOut, SIGNAL(finished()), SLOT(slideOutFinished()));
connect(slideOut, SIGNAL(finished()), slideOut, SLOT(deleteLater()));
}

void slideOutFinished() {
qApp->quit();
}
};


int main(int argc, char **argv)
{
QApplication app(argc, argv);
SelfAnimator w;
w.show();
return app.exec();
}
#include "main.moc"

WetCode
14th January 2013, 23:28
Hello and thanks for your great reply i was shure it was a bether \ correct way to do this.
But am guesing am not that far in to the book`s and Qt yet. Thanks again you gave me a bit of a boost.
Today I have been strugeling to understand chapter 3 in C++ GUI Programming with Qt 4 (2nd) "MainWindow" fully.

So it was nice to see yet another Qt :O
EDIT: Correct me if am wrong but the SelfAnimation class is your making right?

Cheers
WetCode

ChrisW67
14th January 2013, 23:43
Yes, SelfAnimator is mine. It just demonstrates one way you may use the Qt animation framework.