The code you are showing looks alright, at least the following line:
viewer.setMainQmlFile("qrc:/main.qml");
The error is likely somewhere else. You have not shared the project file of yours, but the reason is probably that you do not have this file correctly in your resource.
You would need to use the following entry in your qmake file:
RESOURCES = main.qrc