Page 1 of 1

Beginning Qt Programming pt.2: Platforms about platform independent code. Rate Topic: -----

#1 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Posted 09 August 2009 - 06:24 PM

Introduction
If you have read my last tutorial, you have gotten a basic understanding of C++ and/or Qt. I also said that we we're gonna discuss how to make a GUI, but I'm sorry, theres still more I have to explain before we get to that. In this tutorial, I will be explaining a bit of Platform Specific code. In my own words, Platform Specific code is code that runs only on certain platforms. Examples of platforms are:
  • Windows (Vista, XP, 7, 2000, etc)
  • Mac OS X (10.1, 10.2, 10.3, etc) 'I forgot the names.
  • Linux (Ubuntu, Sabayon, Debian)
  • Unix
Now, that we know some platforms, lets answer a few questions before we get started.
Question 1: What do I do if Qt isn't available for my platform?
Well...I'm not sure, but from what I've heard, its easy to port Qt another platform, but I doubt there isn't a version for your platform.
Question 2: Can I use Qt to program on phones?
YES!!! Qt is also available for phones and embedded systems.
Question 3: Is it free to distribute Qt applications?
Yes! Under the LGPL license, you have to make source-code available if your use Qt for Free, or else you have to pay big $$$.
Question 4: How do i get a makefile?
Included in Qt is a tool called QMake, which takes your Qt Creator project file (.pro) and creates a MakeFile from a set of rules in the pro file.
Coding
Now....Its time to get to discussing the code. As I said before, Qt is available on different platforms but on some of them, the impletetation isn't the same. Thats why Qt allows you to try different code depending on the platform and this is helped by QMake.
An example of your project file/Qmake file is.
#-------------------------------------------------
#
# Project created by QtCreator 2009-07-31T12:58:43
#
#-------------------------------------------------

QT	   -= gui

TARGET = HelloWorld-DIC
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp


Explanation
Heres a short explanation of some common QMake variables.
  • QT = A varible that tells QMake what Modules to include in the build.
  • TARGET = The output file (without the extension)
  • CONFIG = Tells what the app is gonna be.
  • TEMPLATE = Somewhat the same as CONFIG
  • SOURCES = the source code files in the project.
  • HEADERS = the headers of the project.
  • FORMS = the ui files of the project.
Now, a little advanced qmake file for multi-platforms would look like this:
CONFIG += qt debug
 HEADERS += hello.h
 SOURCES += hello.cpp
 SOURCES += main.cpp
 win32 {
	 SOURCES += hellowin.cpp
 }
 unix {
	 SOURCES += hellounix.cpp
 }


Thats a example of a Qmake file that supports multi-platforms. Sounds simple huh?! It may look like it but I've still haven't got the hang of gcc on Linux, oh well.....*<- OFF TOPIC :P *
Stopping QMake if a file doesn't exist.
All though I haven't written the code for the multi-platform qmake code, or the code below, I will give credit to Nokia and the Qt Creator help files. I haven't tested this feature myself yet, but I think it will be good to discuss this.
!exists( main.cpp ) {
	 error( "No main.cpp file found" )
 }

Really, all that code does is checks if the file exist, and if it doesn't, it calls "error" which prints out "the file doesn't exist" and stops qmake. If you would like to print out text without stopping qmake, just use "echo" instead.
Platform Notes
Some stuff, I would like to point out are:
  • Each platform uses a different compiler.
  • The Qt implementation slightly differs platform to platform.
An example of implementation differences are, under linux, x doesn't support windows that are "on top of everything".
Closing
Well, this is the end of this tutorial, in the next part, will talk about some modules and the UI.

Is This A Good Question/Topic? 1
  • +

Replies To: Beginning Qt Programming pt.2: Platforms

#2 eaglex  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 31-May 11

Posted 01 June 2011 - 10:09 AM

where are the other tutorials? :D


btw, thanks for this. I now actually understand some QT basics ;D
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,268
  • Joined: 31-December 10

Posted 08 June 2011 - 09:13 AM

You don't even need an IDE for Qt. All you need is the Qt framework, and a text editor. If you create a source file that uses the Qt framework, then just open the console, go to the directory where the source file is, and type:
qmake -project
qmake
make


The first line will create a .pro file for your project. The next line reads the .pro file and creates a Makefile based on the project file. The last line actually compiles the program, which is nice because you don't need to know all the files that need to be linked to your program. The reasoning behind this is because of the QMetaObject system. This system allows the programmer to add "Signals and Slots" which are just like callback functions. The Signal is usually an event, like the user clicking a button. The Slot is the function that gets called because of some event occurring. The QMetaObject system also adds some other functionality like Run-time type identification without using the built-in RTTI functions. It also allows for the QProperty system as well. This all sounds complicated but because of Qt's macro system, they make it fairly simple to implement. For more info, just check out the Qt website. They have a lot of good info, examples, and tutorials.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1