Page 1 of 1

Beginning Qt Programming pt.1 How to get started! Rate Topic: ***** 1 Votes

#1 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

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

Posted 31 July 2009 - 11:07 AM

Introduction
Qt (pronounced as the English word "cute") is a graphical toolkit for C++. It allows you to write cross-platform applications quicker than standard porting of a applications. Qt is used in many projects like Google Earth, Adobe Photoshop Album, and KDE are good examples. It contains many modules that allow you to get started with Qt Programming. Heres a list.
  • QtWebkit - a Qt version of WebKit that passes the Acid3 test.
  • QtCore - the core of qt.
  • QtGUI - the gui part of Qt.
  • QtXML - Xml related functions in qt.
  • QtSVG - allows you to work with svg files.
And many more!
Getting the Tools
You really don't need a specific tool to develop with Qt. All you need is a IDE (Visual Studio + the sdk for example) to use it. But Nokia has developed a IDE of there own called Qt Creator which allows you to use a form designer, code editor, and qmake all in the same applications. Plus it has a integrated debugger. So head on over to QtSoftware.org and click "Free/LGPL". Select the sdk version depending on your Operating System. They have Mac OS X, Windows, and Linux. Once you have the sdk downloaded and installed, fire up Qt Creator.
Explaining C++
If you know the basics of C++, you may skip this sections. This section is targeted mainly at C# and Java users. Like those languages, C++ uses a C-style format. Heres a basic example in all three languages.
C#
public class Apple : Food
{
	 public void Grow()
	 {
	 }
}

Java
public class Apple : Food {
	   public void Grow() {
	   }
}

And lastly, C++ (without a header)
class Apple : public Food
{
	  void Grow()
	  {
	  }
}

Very little difference huh? Well, theres a few more things I have to discuss.
Headers
For me, it was very hard to find a decent definition of a header. Then as I looked at a header one day, I saw that it was like a "Interface" but for C++, thats when it "Clicked". But, as a down-to-earth example of a header. A header (*.h) is like the skin of an orange while the source file (*.cpp) is the inside where all the juiciness is... umm.... :P A header may look like this.
orange.h
class Orange : public Food
{
	  public:
	  Orange(); //this is the constructor
	  virtual ~Orange(); //this is the deconstructor.
	  void Grow();
};

while its Source file may look like this.
orange.cpp
#include <orange.h>
orange::Orange()
{
}
orange::~Orange()
{
}
orange::Grow()
{
}

And thats basically what a header is...
Pointers
Allthough I still haven't wrapped my head around this, I have a basic understanding what its for. A pointer is a "pointer" (duh!) to another object that has been declared. It's really a arrow pointing to another object and where its stored in the computer's memory. Like in this example.
QString f("This is a string"); //This is a string.
QString* f2 = new QString(f);//This is a pointer to f.

While "f" might be "This is a string", f2 might be 0110F02 or something like that. Its basically a arrow...
Are we there yet?
Yes! Now where gonna start out with a little console application.
Open up Qt Creator, click Develop on the first page. Next, click "Create New Project". A dialog should show up saying what kind of project you want to make. Click "Qt4 Console Application". Now give the project a name, use "HelloWorld" in this example and give it a directory to be stored. The next page should ask you what modules do you want to include, just click QtCore and click next. Finally, it will show a quick overview of your project and you can click finish.
Now you should see a sidebar to the left that has "HelloWorld.pro" and "main.cpp". The ".pro" file is the project file/qmake file while the "main.cpp" is the file where the console will startup from.
Double click the "main.cpp" file. It should already contain something like this.
#include <QtCore/QCoreApplication>

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	return a.exec();
}

In a Qt program, QCoreApplication is always declared first, allowing your application to run. Now, above
int main
, type
using namespace std;
. Now your code should look like this.
#include <QtCore/QCoreApplication>
using namespace std;
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	
	return a.exec();
}


That "std" namespace allows you to print text to the console.
Now add
cout << "Hello World!" << endl;
under the QCoreApplication declaration and it should look like this.
#include <QtCore/QCoreApplication>
using namespace std;
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
		cout << "Hello World!" << endl;
	return a.exec();
}

Now hit CTRL+R (In windows, Command + R in mac i believe) to run your app. In my next tutorial, we will go over QPad, a basic text editor and how you can make it! :P

This post has been edited by Core: 24 August 2009 - 09:35 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Beginning Qt Programming pt.1

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 02 August 2009 - 03:14 AM

Nice Work. :^:
Looking forward to reading your next tutorial.
Was This Post Helpful? 0
  • +
  • -

#3 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

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

Posted 02 August 2009 - 06:51 AM

View Postborn2c0de, on 2 Aug, 2009 - 03:14 AM, said:

Nice Work. :^:
Looking forward to reading your next tutorial.

Thank you, i look forward to it as well :P
Was This Post Helpful? 1
  • +
  • -

#4 alainstgt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 09

Posted 04 August 2009 - 04:24 AM

your definition or better explanation about pointers is strictly not correct.
Why using printf() in a C++ program? you might better use cout!
Alain
Was This Post Helpful? 0
  • +
  • -

#5 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

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

Posted 06 August 2009 - 02:35 PM

View Postalainstgt, on 4 Aug, 2009 - 04:24 AM, said:

your definition or better explanation about pointers is strictly not correct.
Why using printf() in a C++ program? you might better use cout!
Alain

Thank you pointing that out. I will update the tutorial ASAP.

This post has been edited by Amrykid: 09 August 2009 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

#6 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

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

Posted 24 August 2009 - 08:49 AM

just noticed that i have some errors...
Was This Post Helpful? 0
  • +
  • -

#7 Guest_max*


Reputation:

Posted 04 May 2010 - 03:02 PM

View Postalainstgt, on 04 August 2009 - 03:24 AM, said:

your definition or better explanation about pointers is strictly not correct.
Why using printf() in a C++ program? you might better use cout!
Alain

Because cout is pathetically slow, that's why. If you don't care about that, okay.
But printf() is many many times faster than cout.
Was This Post Helpful? 0

#8 Guest_mmessenger*


Reputation:

Posted 04 May 2010 - 05:19 PM

Thanks -testing
Was This Post Helpful? 0

#9 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Posted 10 May 2010 - 02:00 PM

You forgot to include iostream header file, otherwise cout will not work.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Guest*


Reputation:

Posted 04 February 2011 - 04:20 AM

/home/etidhor/c/HelloWorld-build-desktop/../HelloWorld/main.cpp:: In function ‘int main(int, char**)’:
/home/etidhor/c/HelloWorld-build-desktop/../HelloWorld/main.cpp:6: error: ‘cout’ was not declared in this scope

i had a problum whan clicked to debug...............
Was This Post Helpful? 0

#11 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Posted 04 February 2011 - 11:06 AM

Read my reply above yours.


View PostGuest, on 04 February 2011 - 01:20 PM, said:

/home/etidhor/c/HelloWorld-build-desktop/../HelloWorld/main.cpp:: In function ‘int main(int, char**)’:
/home/etidhor/c/HelloWorld-build-desktop/../HelloWorld/main.cpp:6: error: ‘cout’ was not declared in this scope

i had a problum whan clicked to debug...............

Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Posted 08 June 2011 - 09:02 AM

I would like to add that if you don't want to use the Standard C++ library and would rather print to the console using Qt's framework, then include <QDebug> and call the qDebug() function like this:
#include <QtCore/QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "Hello, World!";
    return a.exec();
}


Note that qDebug works on QDataStream which is similar to the Standard streams except for this you need to put the parenthesis "()" after qDebug. Also, you don't need to put a '\n' or an 'endl' because Qt will do it for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1