Alright so is there any way to make this program randomly change the variables x and y every time the button is clicked i am new to programming…
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtGUI>
#include <QWidget>
#include <cstdlib>
#include <ctime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
QPushButton *MainInter = new QPushButton("Push me!",window);
QPropertyAnimation *animation = new QPropertyAnimation(MainInter, "pos");
animation->setDuration(0);
animation->setEndValue(QPoint(x,y));
Object::connect(MainInter,SIGNAL(released()),animation,SLOT(start()));
window->resize(900,500);
window->show();
return a.exec();
}
What you can do is, instead of connecting the
released()signal of your button directly to your animationsstart()SLOT, you would create your own custom SLOT. Then you connect the button to it, handle the action, and call the animation.First read up on how to create a custom QWidget, instead of creating top level object in your
main(). Simple example hereA custom widget might look like this:
widget.h
widget.cpp
And now your
main.cppcan be reduced to the boilerplate code:Every time you click, your custom slot will handle the action and do the animation.