Getting Started on the Commandline: Difference between revisions
No edit summary |
Kamilosxd678 (talk | contribs) (The original CLI code didn't compile. Which was a little embarassing. Fixed it to be able to compile) |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:HowTo]] | |||
{{LangSwitch}} | |||
==Introduction== | |||
Welcome to C++! This guide will show you how to get started with C++ application development with Qt. Before you continue you may want to start downloading the Qt SDK from [http://qt.io here]. Choose the Community version if unsure. The source code examples in this guide are entirely LGPL compatible. On Linux you can also just install libqt4-dev and g++ using your favorite package manager. | |||
=="Hello, world!" console application== | |||
Let's begin with a simple C++ program. Open a text editor and enter the following source code. Keep care to type it exactly like shown in the following example. Like most programming languages C++ is case-sensitive. | |||
<syntaxhighlight lang="cpp"> | |||
#include <QTextStream> | #include <QTextStream> | ||
int main() { | int main() { | ||
QTextStream(stdout) << "Hello, world!" << endl; | QTextStream(stdout) << "Hello, world!" << Qt::endl; | ||
return 0; | return 0; | ||
} | } | ||
</ | </syntaxhighlight> | ||
Create a directory "hello" and save the source code into a file hello.cpp residing in this directory. | Create a directory "hello" and save the source code into a file hello.cpp residing in this directory. | ||
Line 25: | Line 25: | ||
So, on a bash command prompt you would enter: | So, on a bash command prompt you would enter: | ||
== "Hello, world!" desktop application == | $ cd my/dir/hello | ||
$ qmake -project | |||
$ qmake | |||
$ make | |||
$ ./hello | |||
=="Hello, world!" desktop application== | |||
Simply replace the source code in hello.cpp by: | Simply replace the source code in hello.cpp by: | ||
< | <syntaxhighlight lang="cpp"> | ||
#include <QtGui> | #include <QtGui> | ||
#include <QApplication> | |||
#include <QLabel> | |||
int main(int argc, char **argv) { | int main(int argc, char **argv) { | ||
Line 44: | Line 47: | ||
return app.exec(); | return app.exec(); | ||
} | } | ||
</ | </syntaxhighlight> | ||
Add the following lines to the .pro file after the include path:<syntaxhighlight lang="text"> | |||
... | |||
INCLUDEPATH += . | |||
QT += gui | |||
QT += widgets | |||
... | |||
</syntaxhighlight> | |||
Run "make" again. If you launch the application should see a small window saying "Hello, world!". | Run "qmake" and then "make" again. If you launch the application should see a small window saying "Hello, world!". | ||
At this point I hope you were able to take the first step in C++ development using Qt. You may now want to start reading some of Qt's documentation. Qt's API reference is directly written by the software developer that create Qt. You find the latest API documentation | At this point I hope you were able to take the first step in C++ development using Qt. You may now want to start reading some of Qt's documentation. Qt's API reference is directly written by the software developer that create Qt. You find the latest API documentation at [http://doc.qt.io doc.qt.io]. For instance go to {{DocLink|QLabel}} to learn more about the "QLabel" class. Another good starting point is to take a look at the examples, which ship with Qt. If you have chosen to install the Qt SDK and you launch the Qt creator you can open the examples directly from the start page. |
Latest revision as of 21:47, 6 August 2023
Introduction
Welcome to C++! This guide will show you how to get started with C++ application development with Qt. Before you continue you may want to start downloading the Qt SDK from here. Choose the Community version if unsure. The source code examples in this guide are entirely LGPL compatible. On Linux you can also just install libqt4-dev and g++ using your favorite package manager.
"Hello, world!" console application
Let's begin with a simple C++ program. Open a text editor and enter the following source code. Keep care to type it exactly like shown in the following example. Like most programming languages C++ is case-sensitive.
#include <QTextStream>
int main() {
QTextStream(stdout) << "Hello, world!" << Qt::endl;
return 0;
}
Create a directory "hello" and save the source code into a file hello.cpp residing in this directory.
The download of the Qt SDK or package install should be finished by now.
Open a shell and inspect which version of Qt you have installed: enter "qmake -v". If qmake can't be found you have to add its installation path to your environment variable. Search for it in the SDK installation directory.
Now enter the "hello" directory and type: "qmake -project". This will create an qmake project file. Thereafter run just "qmake" without arguments. this will create a "Makefile" which contains the rules to build your application. Then run "make" (called nmake or gmake on some platforms), which will build your app according to the rules layed out in the "Makefile". Finally you can run the app "./hello".
So, on a bash command prompt you would enter:
$ cd my/dir/hello $ qmake -project $ qmake $ make $ ./hello
"Hello, world!" desktop application
Simply replace the source code in hello.cpp by:
#include <QtGui>
#include <QApplication>
#include <QLabel>
int main(int argc, char **argv) {
QApplication app(argc, argv);
QLabel label("Hello, world!");
label.show();
return app.exec();
}
Add the following lines to the .pro file after the include path:
...
INCLUDEPATH += .
QT += gui
QT += widgets
...
Run "qmake" and then "make" again. If you launch the application should see a small window saying "Hello, world!".
At this point I hope you were able to take the first step in C++ development using Qt. You may now want to start reading some of Qt's documentation. Qt's API reference is directly written by the software developer that create Qt. You find the latest API documentation at doc.qt.io. For instance go to QLabel to learn more about the "QLabel" class. Another good starting point is to take a look at the examples, which ship with Qt. If you have chosen to install the Qt SDK and you launch the Qt creator you can open the examples directly from the start page.