Qt toolkit

From NoskeWiki
Jump to: navigation, search

About Qt

Qt is a commercial cross-platform application development framework made in C++, although it also includes bindings for Python, Ruby, PHP, C, Perl, Pascal, C# and Java.


Code

I first coded with Qt in C++ back around 2006 and quickly loved it! Here's some reusable code pieces I'm most proud of:


Related/Child Pages:

Child Pages:

  • Code - qt custom input dialog - lets you generate a customization dialog on the fly with multiple inputs with minimum code lines and without needing to create a class for each dialog.


Licencing

It is possible to install Qt for free is you agree to a LGPL (GNU Lesser General Public License) which more-or-less says you'll make your source code open and not make a cent. The alternative is to pay >US $3700 for a single Commercial Liscence see pricing, depending on how many platforms you want to compile on. (price last updated: Dec 2010)


Installing Qt on Windows

Since I've never been able to afford a commercial licence these instructions are all to install Qt for free! Fortunately it has also become easier to install the free version over the years. The latest versions (v 4.7 and above) are more-or-less plug and play (with minimum instructions needed), but if you run into problems it may help to consult the instructions below.


Qt v4.6 with Visual Studio 2008

To install watch this youtube or follow these instructions:

  • Go to the Qt download page: http://qt.nokia.com/downloads, download "Qt libraries 4.X.X for Windows (VS 2008)" and execute.
    • NOTE: "Qt SDK for Windows" includes Qt Creator, but doesn't work with VS
  • Change the Environment Variables by going: Start > Control Panel > System > Advanced system settings > Advanced > Environment Variables then enter these three pairs of values:

To user variables:

QMAKESPEC
win32-msvc2008

To user variables:

QTDIR
C:\Qt\4.6.2

To existing "Path" system variables add this to the very end (without removing anything):

;%QTDIR%\bin\
  • On your desktop create a new folder called "MyProject"
  • In this folder use Notepad to create "main.cpp" with this text:
#include <QtGUI>
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  QWidget frm;
  frm.setGeometry(100,100,800,600);
  frm.setWindowTitle("Hello World Window");
  frm.show();
  return app.exec();
}
  • Open the special Qt command prompt by going: Start >> All Programs >> Qt 4.6.2 >> Qt 4.6.2 Command Prompt, and then enter this:
cd C:\Users\ANoske\Desktop\MyProject\
qmake -project
qmake -tp vc
  • This creates a "MyProject.pro" and the last command creates a "MyProject.vcproj" with all the build info necessary in the project properties. Open the .vcproj file in Visual Studio 2008 and it should compile when you hit "Start Debugging" [F5]. Good luck!


Qt v4.3

I tried installed an commercial evaluation version once, which worked great and integrated well with Microsoft Visual Studio .NET (MSVS), but obviously expired after 30 days and gave annoying error messages which were difficult to get rid of whenever I opened MSVC. Fortunately, as of version 4.3 the open source version can work with MSVS using these simple steps instructions:

  • Download MigGW source matching qt-win-opensource-4.3.3-mingw.exe from here
  • Run the executable, follow the prompts, and tell it to unpack the library to a good folder: I use C:\Special\Qt\4.3.3
  • Add Qt bin directory (i.e. ";C:\Special\Qt\4.3.3\bin") to your PATH Environment Variable.
  • Read the INSTALL file under C:\Special\Qt\4.3.3\INSTALL
  • Open Visual Studio .NET 2003 Command Prompt (should be under "Visual Studio .NET Tools" in start folder)... and within the prompt type:
    • cd C:\Special\Qt\4.3.3                 (changes to correct dir)
    • configure -help                 (shows important configuration options)
    • configure                 (prepares qt for compilation with specified options)
    • nmake                 (compiles Qt - this step can take a long time)


The only problem I had with their instructions was getting the error message "'nmake' is not recognized as an internal or external command".

To make nmake work the two options seem to be:

  1. Always use "Visual Studio .NET 2003 Command Prompt" (under All Programs >> MS Visual Studio .NET >> MSVS Tools).
  2. Cut and paste the contents of vsvars32.bat into a new cmd window to make the change to environment variables permanent.


NOTE: If you go:

cd C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools
vsvars32.bat
cd C:\Qt\4.3.3

Then nmake will work, but seems to stop working as soon as you close the window


Qt v4.2 or earlier

Prior to v 4.3 it was hard to integrate the free Qt using Visual Studio .NET, and so you needed to download a patch. I installed the evaluation version of 4.2.3 once, which was great, but obviously expired after 30 days and gave annoying error messages which were difficult to get rid of. I had trouble installing the open source 4.2.3, but then came across this page: "Qt4 with Visual Studio" - which helped me succeed.

Quick Summary:


  1. Download source: qt-win-opensource-src-4.2.3.zip
  2. Download patch: acs-4.2.3-patch1.zip
  3. Unzip "qt-win-opensource-src-4.2.3.zip" to good folder: I used C:\Special\Qt\4.2-msvc2003
  4. Add Qt bin directory (i.e. ";C:\Special\Qt\4.2-msvc2003\bin") to your PATH environmental variable.
  5. Unzip "acs-4.2.3-patch1.zip" to same folder.
  6. Open Visual Studio .NET 2003 Command Prompt (should be under "Visual Studio .NET Tools" in start folder) and using this prompt type:
    1. cd C:\Special\Qt\4.2-msvc2003 (changes to correct dir)
    2. installpatch42.bat (installs patch allowing compiling using MSVS)
    3. qconfigure.bat msvc.net (prepares qt for compilation with MSVS 2003) NOTE: if you want to use a different version the argument will be different - type "configure.exe --help"
    4. nmake (compiles Qt - this step can take a long time) Note: may need to copy gl.h up a directory


Using the free Version of Qt within Visual Studio Projects

Once properly installed, the command "qmake" will work. To create a Visual Studio project, make a file called "_Make_Project.pro" with the contents:

######################################################################
# QMAKE FILE FOR CREATING A VISUAL STUDIO PROJECT
######################################################################
 
HEADERS       = 	*.h
SOURCES       = 	*.cpp 	\
			main.cpp
 
QT           += 	opengl       # If you want openGL support.
 
TEMPLATE = vcapp                  # Tells qmake to make a VC application project.
TARGET = MyProgram                # The name of the project.

Now open an instance of "Visual Studio .NET 2003 Command Prompt" (I like to keep a shortcut to it in the same directory), go to that directory and type "qmake". This will generate all the necessary files. Open MyProgram.vcproj and it should compile. Notice it uses/generates several "moc" files during compilation - you *may* have to rerun "qmake" when you want to add new files to the project.


Using the Visual Studio Qt Form Designer in the Evaluation Version

If you install the 30 day free evaluation Windows version it will completely set up Qt for you and integrate a great form designer into Microsoft Visual Studio. This will expire in 30 days, but is useful for rapidly making interfaces. For each QMainWindow it will create:

  • windowname.h - will includes slots, but not the actual elements... but will include "ui_windowname.h"
  • windowname.cpp - slots etc.... but not code to generate the interface - this is kept in a variable called "ui"
  • windowname.ui - this is the "User Interface" in XML, which is used by the interface designer and to generate gui code.

During compilation it will make:

  • ui_windowname.h - THIS file has all the widgets and code to set them up.


NOTE: While the evaluation is still working everything will compile automatically... afterward you will have to install the free version. To compile "ui_window.h" files from the .ui files you can open "Visual Studio .NET 2003 Command Prompt" and run:

uic windowname.ui > ui_windowname.h

Then run qmake and it should all work - maybe requiring a little fiddling. WARNING: You may notice "ui_windowname.h" has been automatically generated with DOUBLE SPACING. For some reason if you remove some of the blank lines at the top and bottom it will no longer compile.


Installing Qt on MAC

  1. Download "qt-mac-opensource-src-4.2.2.tar.gz" from the Qt site.
  2. Unpack the file (doubling click it should work) and I would suggest putting it somewhere like /Developer/Qt/ because moving it later *can* sometimes break it.
  3. Follow the INSTALL instructions.... which are also here: Qt Installation Instructions
./configure && make && sudo make install
    1. Note: When you "install it" it will compile everything in that directory. Only by running make install does it seem to fully install/copy stuff under /usr/local/Trolltech/Qt-4.2.2/bin - but I recommend you still leave the original directory so you can access Designer and ensure nothing breaks.

One of the cool things is that it comes with QtDesigner (this will appear in the bin directory after compilation). I also had problems changing PATH, but eventually I got qmake working. (see: Mac - unix terminal)

Look through the "demo" files and you should be able to build any of these applications by running "qmake" and then "make" in the same directory as the .pro file. To make your own application the you will need to create a .pro file which includes all your source code... something like this should do it:

######################################################################
# QMAKE FILE FOR CREATING A MAKEFILE   (SUITABLE FOR MAC)
######################################################################
TEMPLATE = app
HEADERS   = *.h
SOURCES   = *.cpp

After this I just ran qmake (to create "Makefile") and then make and it compiled the executable. To clean the extra files you can run make clean.

To make a standalone application that will run on ANY Mac computer I've found very tricky. There are instructions here, but I had no success so instead have to rebuild qt on each machine.

Links