Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8443457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:08:38+00:00 2026-06-10T09:08:38+00:00

Alright so is there any way to make this program randomly change the variables

  • 0

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();
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:08:39+00:00Added an answer on June 10, 2026 at 9:08 am

    What you can do is, instead of connecting the released() signal of your button directly to your animations start() 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 here

    A custom widget might look like this:

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    
    class QPushButton;
    class QPropertyAnimation;
    
    class MyWidget : public QWidget
    {
        Q_OBJECT
    public:
       MyWidget(QWidget *parent = 0);
    
    private:
        QPushButton *button;
        QPropertyAnimation *animation;
    
    public slots:
        void randomizeAnim();
    };
    
    #endif // WIDGET_H
    

    widget.cpp

    #include "widget.h"
    #include <QPushButton>
    #include <QPropertyAnimation>
    #include <ctime>
    
    MyWidget::MyWidget(QWidget *parent) :
        QWidget(parent)
    {
        button = new QPushButton("Push me!", this);
        animation = new QPropertyAnimation(button, "pos");
        animation->setDuration(0);
    
        QObject::connect(button, SIGNAL(released()), this, SLOT(randomizeAnim()));
    }
    
    void MyWidget::randomizeAnim()
    {
        srand(time(0));
        int x = 1+(rand()%900);
        int y = 1+(rand()%400);
    
        animation->setEndValue(QPoint(x,y));
        animation->start();
    
    }
    

    And now your main.cpp can be reduced to the boilerplate code:

    #include <QApplication>
    #include "widget.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QWidget *window = new MyWidget;
        window->resize(900,500);
        window->show();
    
        return a.exec();
    }
    

    Every time you click, your custom slot will handle the action and do the animation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright everyone, is there a way to have 2 divs taking up 50% of
Is there any way to get the path of documents currently open in Microsoft
I'm just wondering if there's any way for WPF application integrated with windows live
Is there any way in OpenGL to load and read a 10 bit image?
Is there a way I can store when was the last time a user
Alright, so i have an Activity. In the activity there is a textview and
Alright this is my first day with JQuery so have fun with these functions
Alright I don't see why this isnt working. It seems pretty simple. Here is
Alright, this is driving me nuts because my regex is working on Rubular, but
Alright so I have no idea how to even begin doing this But basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.