I've been to both but not in the same trip. However, I have experienced Bucharest to Belgrade by train, which is only 50km more and was an overnight journey.
Prague to Budapest by train is around 7hrs. At quick glance, there are a number of departures including an overnighter. Some are direct travelling through Slovakia, whereas others include a change in Vienna. Personally, unless you are wanting to save costs and take the lengthier overnight train, I would definitely look to break the journey in Vienna. Although if I had a few days to kill, for a wider experience, I would probably look at something like: Prague > Ceske Budejovice > Linz > Vienna > Budapest. Each stage is around 2hrs'30 tops too.