Melt 4 tbsp butter in a large heavy pot over medium low heat. Add the onions, leeks, shallots and a couple pinches of salt. Stir, and then cover pot and cook for 1 hour, stirring a few times. Onions will reduce by half and be very limp.
Take lid off pot, add in sugar and crank pot up to high heat. Cook, stirring frequently, for about 15 minutes. The juices will brown and stick to the sides of the pot. Use a wooden spoon to scrape browned bits back into the onions, making sure to scrape the bottom of the pot as well. Once the onions have turned creamy and light brown in color turn the heat off and cover the pot.
Heat oven to 350.
Spread the cubed bread onto a baking sheet and bake until lightly toasted, about 15 minutes.
Bring the stock to a boil. Add the bread to the onions and stir. Scrape bread and onion mixture into a wide casserole. Use the stock to rinse out the onion pot and then pour the stock into the casserole dish. Let rest for 5 minutes. The bread will have absorbed some of the stock, but there should still be enough to barely cover the bread. If not, add in some hot water.
Spread the cheese over the top and sprinkle with brandy. Dot with the remaining butter. Bake in oven for 1 hour, or until cheese has formed a rich, dark crust.