You cannot do anything that requires the Qt event loop in main(), because the event loop is not started until you call QApplication::exec() (which you don't do until the end of your main()).
All of the printing code needs to be moved somewhere into your MainWindow class (not the constructor, because exec() still hasn't been called at that point).
If you don't want to go to the trouble of creating a menu with a print command on it, put the printing code inside some slot in MainWindow. Add a signal (anything at all) to MainWindow, then add a handler for the showEvent. In the constructor, connect the signal to the printing slot. In showEvent, fire the signal. At that point, the event loop will be fully up and running and you can test your printing code.
Bookmarks