Click here to Skip to main content
13,193,770 members (45,035 online)
Click here to Skip to main content
Add your own
alternative version

Stats

33.9K views
37 bookmarked
Posted 4 Jun 2005

Development of Applications of Cross-Platform with Qt and MSVC++ - Part 1

, 4 Jun 2005
Rate this:
Please Sign up or sign in to vote.
How to develop programs using MSVC++ and Qt for Windows, Linux and MacOS

Introduction

With this article, I intend to begin a series of articles on multi-platform development of software using C++ as the language and the framework Qt.

I have been developing software for a few year and I always try to maintain my programs. This is not an easy task, because to accomplish this task, it is necessary to define which operating systems will be accepted.

I usually work with a simpler group of systems (Windows, Linux and Palm OS), therefore the task that seems easy it is actually arduous.

In this article, I will present the tools used to develop programs carried using the Qt library. I won't flee the rule of the "Hello world!" (the apple for the designers).

First Program

I will give the example of the traditional "Hello World" so that one can have a fast idea of the library and also the traditional program for console.

//CODE: hello.cpp  
#include <qapplication.h> //QApplication  
#include <qlabel.h> / / QLabel  
  
int main(int argc, char * argv [])  
{  
   / / create the application object and initialize it.  
        QApplication app(argc, argv);  
   / / create the main object/widget required to receive message   
        QLabel * lbl = new QLabel ("Hello World! ", 0);  
   / / it defines the main object/widget  
        app.setMainWidget(lbl);  
   / / we need show it (required)  
        lbl->show ();  
   / / event loop (required)  
        return app.exec ();  
}    

However to compile this code, we need a makefile to build the executable easily. If you are using Qt-3.x or more recent version, you can use the automatic generator that accompanies your own Qt.

I want to make it clear that the previous code can run in three different operating systems with ZERO alteration. You should just compile the code with the appropriate compiler for the wanted operating system. (Win=MSVC++; Linux=GCC; MacOS=CodeWarrior).

In the DOS prompt, you should execute the following commands in the exact same order:

qmake - project - hello.pro  
qmake  
nmake    

The first command will join all of the file sources in the current folder and create a project file. The second command will create the necessary makefile for the construction of the executable. You can call CL.EXE directly, but it is faster to use the generator of Qt. See the content of the project file below generated by qmake:

######################################################################  
# Automatically generated by qmake (1.06c) sáb 4. jun 18:50:03 2005  
###################################################################### TEMPLATE = app  
CONFIG -= moc  
INCLUDEPATH += .  
# Input  
SOURCES += Hello.cpp  

The classic helloworld from Microsoft DOS is:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello world!\n");    
}

If you are using Microsoft Windows, then you will have the file hello.exe that can be run by double clicking of the mouse or through the prompt of Microsoft-DOS. If the used system goes Linux, it will be enough to turn through prompt ./hello or through double click in the manager of Windows in use.

If you are using MAC OS, I recommend you ask somebody, a person who knows the system (this is not me.... I have never used it).

The components of Qt will accept text HTML, therefore you will be surprised when changing "Hello World! " for something of the type "<H1> Hello World! </ H1>. This will work. But you can change the label for other control / widget. See the change below for a button.

//CODE: hello.cpp  
#include <qapplication.h> //QApplication  
#include <qpushbutton.h> / / QPushButton
  
int main(int argc, char * argv [])  
{  
   // create the application object and initialize it.  
        QApplication app(argc, argv);  
   // create the main object/widget required to receive message   
        QPushButton * btn = new QPushButton("Hello World  and Quit! ", 0);  

  //new code to close on button click ;-)

  //We should connect the event Click () to the command Quit () of the application.    

    QObject::connect(btn, SIGNAL(clicked()), &app, SLOT(quit());

   // it defines the main object/widget  
        app.setMainWidget(btn);  
   // we need show it (required)  
        btn->show ();  
   // event loop (required)  
        return app.exec ();  
} 

You must have noticed in the previous code that the commands SLOT() and SIGNAL() are macros that will be explained later.

I am writing this small article to show the 1-2-3 of Qt. There is an IDE that allow us to draw the screens (AppWizard) and to connect the events visually, and soon we can write functional code. This will be the subject of the next article.

I will see you soon!

History

  • 4th June, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

WillianBR
Software Developer
Brazil Brazil
Developer:
- MS VB6
- C/C++/C#
- VBScript
- JScript
- Clipper
- Cobol
- Natural
- Pascal
- TCL/Tk
- Python
___________________________________
Willian Silva Rodrigues
http://www.williansilva.com.br
http://br.linkedin.com/in/willianbr
--- --- --- ---
Radio(NXTL):55*112*109193
BBM PIN: 238d4349
___________________________________

You may also be interested in...

Comments and Discussions

 
GeneralQT class library download Pin
vikas amin23-Nov-05 20:44
membervikas amin23-Nov-05 20:44 
GeneralRe: QT class library download Pin
Willian.BR23-Nov-05 23:44
memberWillian.BR23-Nov-05 23:44 
GeneralRe: QT class library download Pin
vikas amin24-Nov-05 3:10
membervikas amin24-Nov-05 3:10 
GeneralNeed help,, Pin
Md Saleem Navalur14-Nov-05 0:39
memberMd Saleem Navalur14-Nov-05 0:39 
GeneralRe: Need help,, Pin
rm217-Jan-06 22:05
memberrm217-Jan-06 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171018.2 | Last Updated 4 Jun 2005
Article Copyright 2005 by WillianBR
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid