Wednesday, November 8, 2017

Qt Tutorial: Distributing Your Application (.exe) or Running Your Qt Application without Qt

Ok, I know I said I wanted to do a tutorial on how to autoposition and auto resize the widgets when the window is maximized. But, as usual something else more important comes up - distributing an executable to your friends or making the executable run outside of Qt.

I have just learned how to make windows application using Qt. I made a small tool for my own usage. And then I found out that my friend also likes to use it. But I cannot simply copy the EXE and pass to my friend. It simply won't run! Why?

Did you have this question? I did too!

I am used to have a standalone EXE in developing Windows app since I used Lazarus and Delphi before. In XCode too, we have a standalone executable. (ipa). So when faced with weird EXE that cannot execute, my reaction was "WTF IS THIS SHIT" lol! As it turns out, executable written in Qt requires additional files for it to function. Files like .dll (windows dynamic libraries), any .ini files that you use and other related files. And furthermore Qt application has 2 realms that it builds on (that you can select, but probably you didn't notice) - the Debug build and the Release build. These 2 realms are typical. XCode has it too. The difference is, with Qt, both the Debug or Release build is NOT executable by itself, it needs Qt to run.. Dumb right? I know..

So how do you go about distributing your app to your friends or other computers?  Just for this purpose alone, lets make a dumb application. It's going to be an empty window application that does nothing. Cause, why not :D. If you don't know how to make an empty application, refer to the previous tutorial.

1. MAKING EXE RUN BY ITSELF


Once you have create an empty application (Widget type), and saved it to a location, you are ready to proceed. First, of course, you have to select the Release build.



Next try to Run it from Qt. The app should run and show an empty window. Now a DumbApp.exe will appear in your Release folder. But... double click it and nothing happens. It means the app needs Qt to run and only will run from Qt. Isn't it dumb? LOL!

Anyway once you confirmed that your app has no error and able to run from Qt, then you can proceed to next step - that is deploying your qt app using windeployqt.exe tool. Open up Dos Command Prompt and goto your release folder and type:


C:\Qt is where I install my Qt. So you may need to change that to your own Qt folder. Click enter and the program will process your exe and copy related files to the same folder. You'll end up with something like below:

As you can see many dlls and folders are created and added to your release folder now. However! windeployqt.exe seems to be missing a few files that you must manually copy from  C:\Qt\5.9.1\mingw53_32\bin into the release folder. Again, this is assuming you installed your Qt at C:\Qt and you have mingw compiler (which is the default in most Qt installations). These are the 3 files:

- libgcc_s_dw2-1.dll
- libstdc++-6.dll
- libwinpthread-1.dll


At this point, your application should be able to be executed separately from Qt. You can try closing Qt Creator, and doubleclicking the DumbApp.exe in the release folder. You should see it running showing an empty window without Qt Creator! If not, something else is missing. You need to ask around (especially if you're not using Qt5.9) for which additional files you may need to copy manually.

So now you can simply ZIP the whole release folder and send to your friends, or, the more classy way, compile your files into an Installer.exe!

2. COMPILING YOUR APP FILES INTO INSTALLER.EXE


First thing to do is installing Inno Setup. Trust me on this when I say Inno Setup IS THE WAY TO GO. I was hesitant earlier because I didn't want to install another program to distribute the app. But Inno Setup is a neat little tool and also it is absolutely free even for commercial use!

Head over here to download: http://www.jrsoftware.org/isdl.php

Once you installed it, run Inno Setup Compiler and if there is a Welcome dialog box, select Create New Script file using the Script Wizard. If not, then goto the menu and select File->New... The wizard will start. Click Next and fill in your required details like your application name, version number, etc.

There will be a few dialogs to complete. You will come to this dialog:

Click Browse button and select your EXE file in the release folder of your project. Click next and soon you will arrive here:


You need to specify the output folder so that your after InnoSetup compiles your script that was created by the wizard, the resulting Installer.exe will be there. Also change the Compiler output base file name to OTHER than setup. Perhaps Installer.

After a few more dialogs, you can click Finish and Inno Setup will display your created script with the prompt:


You can see the details you entered in the wizard now is inside the script. Take a time to read through the script to get the feel for it. Click No to the prompt. You can compile it now, but your app will still be unable to run. I'll tell you why.

It is because you need to modify the script a little bit. First, in the script you created, under [Files] entry change this:


Source: "G:\QT\build-DumbApp-Desktop_Qt_5_9_1_MinGW_32bit-Release\release\DumbApp.exe"; DestDir: "{app}"; Flags: ignoreversion

To:

Source: "G:\QT\build-DumbApp-Desktop_Qt_5_9_1_MinGW_32bit-Release\release\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs

This way it will include all the files inside release folder and recurse all the folders and subfolders in there. Normally an app would probably have some dlls, iconfiles, user manuals, .ini files, and database files, etc, so this script change will handle all those.

After that, you can click the Inno Setup menu Build->Compile.. Inno setup then will compile and your Installer.exe will be ready in the output folder. The size of installer is normally quite big around 13MB just for our empty window dumb app. If you have more content, the size will be even higher.


Done! And that concludes this tutorial.


Sunday, September 24, 2017

Absolute Beginner Tutorial: Your First Qt Windows Application.

Welcome to A New Tutorial Blog!

First... some whining and rants :P

Coming from Borland Delphi (& Lazarus) and XCode experiences, learning Qt is something relatively new and uncomfortable. Tthe documentation is not newbie friendly. At all. And, there aren't many sites or blog that writes guide and code samples for complete newbies.  They use simplified steps and missing out the crucial steps. Most tutorial sites speak non-newbie language. Even answers in forums are non-newbie-friendly.

Their answers are normally "find a book and read documentation yourself". This is totally opposite of what the XCode community is - who are mostly very generous in teaching others even down to the very basic of Objective C or Swift.

Qt Tutorial: Distributing Your Application (.exe) or Running Your Qt Application without Qt

Ok, I know I said I wanted to do a tutorial on how to autoposition and auto resize the widgets when the window is maximized. But, as usual s...