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 216389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:33:36+00:00 2026-05-11T18:33:36+00:00

My application is developped in C++ using Qt and is using signals and slots.

  • 0

My application is developped in C++ using Qt and is using signals and slots.

Let’s say I have the following classes (pseudo-C++ code) :

class Ball
{
    Color m_Color;
    int m_Size;
};

class Player
{
public:
    setBall(Ball* pBall)
    {
        if (pBall != m_pBall)
        {
            Ball* pPreviousBall = m_pBall;
            m_pBall = pBall;
            emit notifyBallNotUsed(pPreviousBall);
        }
    }

    Ball* getBall();

signals:
    void notifyBallNotUsed(Ball*);

private:
    String m_Name;
    Ball* m_pBall;
};

class GeneralHandler
{
public:
    addBall(Ball* pBall);
    deleteBall(Ball* pBall);


    addPlayer(Player* pPlayer)
    {
        connect(pPlayer, SIGNAL(notifyBallNotUsed(Ball*)), this, SLOT(onBallUsageChanged(Ball*)));
        ...
    }
    deletePlayer(Player* pPlayer);
    {
        disconnect(pPlayer, SIGNAL(notifyBallNotUsed(Ball*)), this, SLOT(onBallUsageChanged(Ball*)));

        onBallUsageChanged(pPlayer->getBall());
        ....
    }

private slots:
    void onBallUsageChanged(Ball* pBall)
    {
        if (isNotUsedAnymore(pBall))
        {
            m_BallList.remove(pBall);
            delete pBall;
        }
    }

private:
    bool isNotUsedAnymore(Ball* pBall); // Check if the given ball is still used by at least one player

    List<Player*> m_PlayerList;
    List<Ball*> m_BallList;
};

With my application, the user can add/remove player, and for each player, decide the color and the size of the ball. Behind the hood, the GeneralHandler is in charge to store the balls and delete them.
It is perfectly possible that two players are using the same ball.

When a player is deleted, if the ball is not used anymore, the GeneralHandler should delete it (or keep it if the ball is still used by another player).
If the ball a player is using is changed, the previous ball, if not used anymore, should be deleted by the GeneralHandler as well.

So far so good.

Now, I want to add undo/redo capability to my application, using the command pattern, and this is where I’m stuck. Lets say I have something like this :

class ChangePlayerBall : public QUndoCommand
{
public:
    ChangePlayerBall(Player* pPlayer, Ball* pNewBall)
    {
        m_pPlayer = pPlayer;
    }

    void redo();
    void undo();

private:
    Player* m_pPlayer;
};

I guess the redo() method will look like this :

void ChangePlayerBall::redo()
{
    m_pPlayer->setBall(pNewBall);
}

If nothing else is changed in the code above, the previous Ball will be deleted if not used anymore by other players.
This will be a problem when implementing the undo() method : if the previous ball has been deleted, I don’t know what was it’s characteristics and the undo command won’t be able to recreate it.
Or maybe I should store the previous ball, but how will the undo/redo command know if this previous ball is still existing or has been deleted by the handler ?
Or maybe this mechanism of deleting a ball as soon as it isn’t used anymore should be implemented in the undo command ? The problem is that the undo command will have a lot of dependencies on many other classes. The other problem is that this code would be partially duplicated in the DeletePlayer command, which will have to do something similar :

class DeletePlayer : public QUndoCommand
{
public:
    DeletePlayer(Player* pPlayer);

    void redo();
    void undo();
...
};

I hope my explainations where understandable !

How would you solve this problem ? I cannot find a satisfying solution.

Thanks !

  • 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-05-11T18:33:36+00:00Added an answer on May 11, 2026 at 6:33 pm

    How about using a reference counting trick for the ball? When a ball is stored in the command, the command can increment the reference count for the ball, hence preventing from being deleted by the handler (or by itself, depending on how you would change the implementation).

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

Sidebar

Related Questions

I have an application that I developped using the iPhone/iPad simulator. I am now
I have a very nice MVC Beta application developed using VS2008 on a win2008
I have a pure C++ application developed using VC 6.0. I would like this
I have a ASP.net 3.5 web application developed in VB.net I am using iTextsharp
We currently have developed an application using WCF. Our clients make connections to different
I have C++ application which has UI developed using MFC, does some networking using
I have this application developed for Blackberry 4.5. I´m using Eclipse, and I want
The scenario: I have a PRISM application developed in Silverlight (4), and I'm using
I have a Qt application that was developed using Qt Creator and the GUI
Hi have this j2ee web application developed using spring framework. I have a problem

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.