Multiple options with 1-2 nights in between! :)
For example Day 1 :
- FA Roma Termini - Genova P. Principe 06:23 - 10:45
- IC Genova P. Principe - Ventimiglia 10:58 - 13:17
- TER Ventimiglia - Nice-Ville 13:47 - 14:41
Later options via Milan available too (for example 07:20 - 16:11).
Day 2 :
- any TER Nice-Ville - Marseille St-Charles 2h45 journey
Day 3 :
- AVE Marseille St-Charles - Barcelona-Sants 08:04 - 12:38
Alternatives with changes in Nîmes or Montpellier possible. Also doable fully by regional trains but it would be another very long day.
Do you have a Interrail/Eurail pass ? The AVE train cannot be booked online but you can get reservations on the platform issued by RENFE staff (10€).
As an alternative there is an overnight ferry from Civitavecchia (near Rome) to Barcelona but it isn't as fun...