qt-info

Upload: priya-nair

Post on 07-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 qt-info

    1/2

    Getting Started With Qt

    Background and documentationYou can read about the evolution and features of the Trolltech Qt framework at these links whichyou should look at in this order.

    Big Picture: http://www.trolltech.com/pdf/whitepapers/qt33-whitepaper-a4.pdfTechnical Stuff: http://doc.trolltech.com/3.2/index.htmlTutorial: http://doc.trolltech.com/3.0/tutorial.html

    The documentation about classes, signals, and slots is ovewhelming but you should scout aroundand see how much you can absorb about writing and running Qt applications. I will describe howto build Qt apps in detail in class and lab so for now figure out what you are going to need to learnin order to build your GUI application.

    Using Qt on the Linux machines on campusThe Qt tools installed on the Linux machines are sufficient to do all our work in building a GUI forour Kalah game. We can edit our files much like before and use the convenient qmake and makecommands to build our applications. A Qt application, something like hello or hello.app, is a filewhich can be (double) mouse clicked and it brings up a window inside which we see widgets(buttons, sliders, images, etc) that inhabit the window content region. Our task is to learnenough about Qt that we know how to define, position, and control these window inhabitants toplay a Kalah game for the user.

    Building Qt projectsEach Qt project, including source files, compiled application, etc, must be contained in a singlefolder. To handle the complex compilation needed for the various libraries, sources, etc, we usea standard Unix tool called make which contains the specific compiler options, dependenciesamong the various source and object files, and so forth. Since Qt classes add an additional layerof complexity, Trolltech has provided a Meta-object compiler, or moc, that builds the graphicslibraries in a consistent manner for us. They also provide a multipurpose tool called qmake usedto create a project file, with extension .pro, which wraps all our file dependencies into one file

    which is then used to create a Makefile which can be normally read by make. Sounds confusingbut is pretty simple in actual use. You will also need to add qmake to your PATH environmentvariables so you can avoid using its entire path name each time you invoke it. I cover that afterthis example of using qmake.

    For an example using qmake, suppose I have a single file main.cpp, which contains my entire Qtprogram, located in folder kalah. To build your application file kalah, simply type these lines:

    qmake project (Creates a project file based on the includes found in main.cpp)qmake (Creates a file called Makefile which make will consult during compilation)make (This does the actual compilation and displays error messages, etc)

    Assuming no errors are found during compilation, in your folder you will now find an application

    called kalah you can run as usual and it will display the window and contents of your application.Note: on Macintosh, you can double click in the Finder or use the command open kalah.app tostart the application.

    Adding to our PATH and other environment variables in LinuxThe application qmake is found in the /usr/lib/qt3/bin directory so you should add its directory pathto your PATH environment variable so you dont have to use the full directory path each time youinvoke qmake. To do this, use nedit, or other editor, to edit the file named .profile found in yourhome directory. Append these lines to your current .profile

  • 8/6/2019 qt-info

    2/2

    QTDIR=/usr/lib/qt3PATH=$QTDIR/bin:$PATHMANPATH=$QTDIR/man:$MANPATHLD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH

    export QTDIR PATH MANPATH LD_LIBRARY_PATH

    Save your .profile file, close your session and login again (a quick way to record environmentvariable changes) and you can now simply use the qmake commands as shown above to buildyour Qt applications inside any directory. NOTE: If you are using Qt on Mac or Windows, consultthe documentation in INSTALL which tells you how to modify your shell .profile or .loginaccordingly.

    Using Qt on your own computerNote that you can download and install the various free versions of Qt on your own machine ifyou wish. Note we will be using version 3.3 of Qt for our examples in class so you man want toinstall version 3.3 to be consistent with our use and examples.

    The instructions for installation found at http://doc.trolltech.com/3.3/installation.htmlare fairly clearbut compiling all the various libraries takes a couple of hours and some close reading of the

    instructions to avoid having to start over. If you have trouble, let me know and I will try to providesuggestions on installation problems. We have Visual C++ for Windows if you wish to do theWindows version. The gcc compiler comes with XCode for the Macintosh which you shouldinstall BEFORE you install Qt.

    Starting to program in QtQt comes with a large collection of examples which provides far too many source files to readand build in the time we have available so we will concentrate on just a few examples togetherwith code from the tutorial. All the examples are found in the directory /usr/lib/qt3/doc/examples.You can copy any or all of these example directories over to your home directory and try them outto see what is available in Qt. Each comes with a precompiled application so you can simple runeach one and see what it does.

    I will be using some of the tutorial code also supplied by Trolltech as a way to build up to ourKalah game application. You should locate and copy the tutorial folder found at/classes/cs226/s06/tutorial into a convenient place in your home directory. You can find anexplanation of all the tutorials at http://doc.trolltech.com/3.0/tutorial.html. Working through thetutorials is a useful way to get acquainted with the graphics hierarchy and signals/slotsmechanism. NOTE: If you wish to start from scratch for each tutorial folder, just delete the .proand Makefile files in each folder and rebuild the application as described above.

    Additional referencesThere is one standard book on Qt 3 which I will leave in the computer lab which you can alsoconsult if the need arises.