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.
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....
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!
This post has been edited by Core: 24 August 2009 - 09:35 AM






MultiQuote






|