If you don't have dedicated transportation, you will need to take the train to London to get from Southampton to Dover anyway, but the journey itself will take you a good 4 hours between ports if all goes well. And I'd allow at least 5hrs to be on the safe side. (1h30 from first port to London + 70 minutes from London to Dover, + wait times + connections + transfer from, and to ports)
Onboard time is 4pm at Dover, meaning you need to check in earlier than that.
Disembarkation time for the QM2 is between 8:45 m and 11am depending on how long it will take you to clear Customs (can be as early as 7:30am if you carry your own luggage off the ship)
you might need to arrange for an early disembarkation time as soon as you're onboard QM2
Depending on that time, and your check in time at Dover, you may just have barely enough time to make it between ports, and this might even be tight, should you disembark late or have any issue on the trains or navigating through stations to make train connections
So I think visiting London between QM2 and
DCL is taking a huge risk, and might not even be doable. (And I would strongly advise against it)