meego qt框架初认识

上一篇 / 下一篇  2011-01-24 22:07:28 / 个人分类:Nokia

qmake Tutorial

This tutorial teaches you how to useqmake. We recommend that you read theqmakeuser guide after completing this tutorial.

Starting off Simple

Let's assume that you have just finished a basic implementation of your application, and you have created the following files:

  • hello.cpp
  • hello.h
  • main.cpp

You will find these files in theexamples/qmake/tutorialdirectory of the Qt distribution. The only other thing you know about the setup of the application is that it's written in Qt. First, using your favorite plain text editor, create a file calledhello.proinexamples/qmake/tutorial. The first thing you need to do is add the lines that tellqmakeabout the source and header files that are part of your development project.

We'll add the source files to the project file first. To do this you need to use the SOURCES variable. Just start a new line withSOURCES +=and put hello.cpp after it. You should have something like this:

SOURCES += hello.cpp

We repeat this for each source file in the project, until we end up with the following:

SOURCES += hello.cpp
 SOURCES += main.cpp

If you prefer to use a Make-like syntax, with all the files listed in one go you can use the newline escaping like this:

SOURCES = hello.cpp \
           main.cpp

Now that the source files are listed in the project file, the header files must be added. These are added in exactly the same way as source files, except that the variable name we use isHEADERS.

Once you have done this, your project file should look something like this:

HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp

The target name is set automatically; it is the same as the project file, but with the suffix appropriate to the platform. For example, if the project file is calledhello.pro, the target will behello.exeon Windows andhelloon Unix. If you want to use a different name you can set it in the project file:

TARGET = helloworld

The final step is to set the CONFIG variable. Since this is a Qt application, we need to putqton theCONFIGline so thatqmakewill add the relevant libraries to be linked against and ensure that build lines formocanduicare included in the generated Makefile.

The finished project file should look like this:

CONFIG += qt
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp

You can now useqmaketo generate a Makefile for your application. On the command line, in your project's directory, type the following:

qmake -o Makefile hello.pro

Then typemakeornmakedepending on the compiler you use.

For Visual Studio users,qmakecan also generate.dspor.vcprojfiles, for example:

qmake -tp vc hello.pro

Making an Application Debuggable

The release version of an application doesn't contain any debugging symbols or other debugging information. During development it is useful to produce a debugging version of the application that has the relevant information. This is easily achieved by addingdebugto theCONFIGvariable in the project file.

For example:

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp

Useqmakeas before to generate a Makefile and you will be able to obtain useful information about your application when running it in a debugging environment.

Adding Platform-Specific Source Files

After a few hours of coding, you might have made a start on the platform-specific part of your application, and decided to keep the platform-dependent code separate. So you now have two new files to include into your project file:hellowin.cppandhellounix.cpp. We can't just add these to theSOURCESvariable since this will put both files in the Makefile. So, what we need to do here is to use a scope which will be processed depending on which platform.qmakeis run on.

A simple scope that will add in the platform-dependent file for Windows looks like this:

win32 {
     SOURCES += hellowin.cpp
 }

So ifqmakeis run on Windows, it will addhellowin.cppto the list of source files. Ifqmakeis run on any other platform, it will simply ignore it. Now all that is left to be done is to create a scope for the Unix-specific file.

When you have done that, your project file should now look something like this:

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp
 win32 {
     SOURCES += hellowin.cpp
 }
 unix {
     SOURCES += hellounix.cpp
 }

Useqmakeas before to generate a Makefile.

Stopping qmake If a File Doesn't Exist

You may not want to create a Makefile if a certain file doesn't exist. We can check if a file exists by using the exists() function. We can stopqmakefrom processing by using the error() function. This works in the same way as scopes do. Simply replace the scope condition with the function. A check for amain.cppfile looks like this:

!exists( main.cpp ) {
     error( "No main.cpp file found" )
 }

The!symbol is used to negate the test; i.e.exists( main.cpp )is true if the file exists, and!exists( main.cpp )is true if the file doesn't exist.

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp
 win32 {
     SOURCES += hellowin.cpp
 }
 unix {
     SOURCES += hellounix.cpp
 }
 !exists( main.cpp ) {
     error( "No main.cpp file found" )
 }

Useqmakeas before to generate a makefile. If you renamemain.cpptemporarily, you will see the message andqmakewill stop processing.

Checking for More than One Condition

Suppose you use Windows and you want to be able to see statement output with qDebug() when you run your application on the command line. Unless you build your application with the appropriate console setting, you won't see the output. We can easily putconsoleon theCONFIGline so that on Windows the makefile will have this setting. However, let's say that we only want to add theCONFIGline if we are running on Windowsandwhendebugis already on theCONFIGline. This requires using two nested scopes; just create one scope, then create the other inside it. Put the settings to be processed inside the last scope, like this:

win32 {
     debug {
         CONFIG += console
     }
 }

Nested scopes can be joined together using colons, so the final project file looks like this:

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp
 win32 {
     SOURCES += hellowin.cpp
 }
 unix {
     SOURCES += hellounix.cpp
 }
 !exists( main.cpp ) {
     error( "No main.cpp file found" )
 }
 win32:debug {
     CONFIG += console
 }

That's it! You have now completed the tutorial forqmake, and are ready to write project files for your development projects.

qmake Tutorial

This tutorial teaches you how to useqmake. We recommend that you read theqmakeuser guide after completing this tutorial.

Starting off Simple

Let's assume that you have just finished a basic implementation of your application, and you have created the following files:

  • hello.cpp
  • hello.h
  • main.cpp

You will find these files in theexamples/qmake/tutorialdirectory of the Qt distribution. The only other thing you know about the setup of the application is that it's written in Qt. First, using your favorite plain text editor, create a file calledhello.proinexamples/qmake/tutorial. The first thing you need to do is add the lines that tellqmakeabout the source and header files that are part of your development project.

We'll add the source files to the project file first. To do this you need to use the SOURCES variable.Just start a new line withSOURCES +=and put hello.cpp after it. You should have something like this:

SOURCES += hello.cpp

We repeat this for each source file in the project, until we end up with the following:

SOURCES += hello.cpp
 SOURCES += main.cpp

If you prefer to use a Make-like syntax, with all the files listed in one go you can use the newline escaping like this:

SOURCES = hello.cpp \
           main.cpp

Now that the source files are listed in the project file, the header files must be added. These are added in exactly the same way as source files, except that the variable name we use is HEADERS.

Once you have done this, your project file should look something like this:

HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp

The target name is set automatically; it is the same as the project file, but with the suffix appropriate to the platform. For example, if the project file is calledhello.pro, the target will behello.exeon Windows andhelloon Unix. If you want to use a different name you can set it in the project file:

TARGET = helloworld

The final step is to set theCONFIGvariable. Since this is a Qt application, we need to putqton theCONFIGline so thatqmakewill add the relevant libraries to be linked against and ensure that build lines formocanduicare included in the generated Makefile.

The finished project file should look like this:

CONFIG += qt
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp

You can now useqmaketo generate a Makefile for your application. On the command line, in your project's directory, type the following:

qmake -o Makefile hello.pro

Then typemakeornmakedepending on the compiler you use.

For Visual Studio users,qmakecan also generate.dspor.vcprojfiles, for example:

qmake -tp vc hello.pro

Making an Application Debuggable

The release version of an application doesn't contain any debugging symbols or other debugging information. During development it is useful to produce a debugging version of the application that has the relevant information. This is easily achieved by addingdebugto theCONFIGvariable in the project file.

For example:

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp

Useqmakeas before to generate a Makefile and you will be able to obtain useful information about your application when running it in a debugging environment.

Adding Platform-Specific Source Files

After a few hours of coding, you might have made a start on the platform-specific part of your application, and decided to keep the platform-dependent code separate. So you now have two new files to include into your project file:hellowin.cppandhellounix.cpp. We can't just add these to theSOURCESvariable since this will put both files in the Makefile. So, what we need to do here is to use a scope which will be processed depending on which platform.qmakeis run on.

A simple scope that will add in the platform-dependent file for Windows looks like this:

win32 {
     SOURCES += hellowin.cpp
 }

So ifqmakeis run on Windows, it will addhellowin.cppto the list of source files. Ifqmakeis run on any other platform, it will simply ignore it. Now all that is left to be done is to create a scope for the Unix-specific file.

When you have done that, your project file should now look something like this:

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp
 win32 {
     SOURCES += hellowin.cpp
 }
 unix {
     SOURCES += hellounix.cpp
 }

Useqmakeas before to generate a Makefile.

Stopping qmake If a File Doesn't Exist

You may not want to create a Makefile if a certain file doesn't exist. We can check if a file exists by using the exists() function. We can stopqmakefrom processing by using the error() function. This works in the same way as scopes do. Simply replace the scope condition with the function. A check for amain.cppfile looks like this:

!exists( main.cpp ) {
     error( "No main.cpp file found" )
 }

The!symbol is used to negate the test; i.e.exists( main.cpp )is true if the file exists, and!exists( main.cpp )is true if the file doesn't exist.

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp
 win32 {
     SOURCES += hellowin.cpp
 }
 unix {
     SOURCES += hellounix.cpp
 }
 !exists( main.cpp ) {
     error( "No main.cpp file found" )
 }

Useqmakeas before to generate a makefile. If you renamemain.cpptemporarily, you will see the message andqmakewill stop processing.

Checking for More than One Condition

Suppose you use Windows and you want to be able to see statement output with qDebug() when you run your application on the command line. Unless you build your application with the appropriate console setting, you won't see the output. We can easily putconsoleon theCONFIGline so that on Windows the makefile will have this setting. However, let's say that we only want to add theCONFIGline if we are running on Windowsandwhendebugis already on theCONFIGline. This requires using two nested scopes; just create one scope, then create the other inside it. Put the settings to be processed inside the last scope, like this:

win32 {
     debug {
         CONFIG += console
     }
 }

Nested scopes can be joined together using colons, so the final project file looks like this:

CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp
 win32 {
     SOURCES += hellowin.cpp
 }
 unix {
     SOURCES += hellounix.cpp
 }
 !exists( main.cpp ) {
     error( "No main.cpp file found" )
 }
 win32:debug {
     CONFIG += console
 }

That's it! You have now completed the tutorial forqmake, and are ready to write project files for your development projects.


TAG: MeeGo meego nokia qt 框架

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-11  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 1698
  • 日志数: 3
  • 建立时间: 2011-01-24
  • 更新时间: 2011-01-24

RSS订阅

Open Toolbar