Topics
Qt Introduction
1) What's Qt?
2) How can we make a Qt console program?
Ch 1.5 – 1.11
& Ch 3
3) How can we use dialogs?
Q: How do you pronounce Qt?
A: This puppy is....
23/01/12
CMPT 212
Slides #5
© Dr. B. Fraser
1
Qt
ì
23/01/12
C++ Build Process
car
.cpp
Qt is:
car.h
ì
:
Strong GUI creation.
Extends C++
(object communication)
Preprocessor
main
.cpp
car.h
Example Applications:
:
:
23/01/12
Header
file
Inserted
or .obj
Compiled
Compiler
gcc.lib
Features:
:
ì
2
Google Earth
K Desktop Environment
Header
file
Inserted
Preprocessor
Compiled
Compiler
Linker
3
23/01/12
4
Qt Build Tools
ì
Create a Qt Project File (.pro)
> qmake -project
Note: For console applications, add this to the .pro file:
CONFIG += console
ì
Create a Makefile
> qmake
ì
Qt Creator
ì
qmake -project
:
myApp.pro
:
:
qmake
ì
make
:
> make
23/01/12
Build Demo
5
C++/Qt Coding (context sensitive help, syntax
highlighting, code-completion)
Designing GUIs
Debugging
Demo:
:
Makefile
Build the application
Qt Creator IDE in Qt SDK:
23/01/12
Creating new project,
adding a class,
importing code.
Changing project settings,
editing .pro file.
http://labs.qt.nokia.com/2011/11/08/qt-sdk-1-1-4-update-available/
6
Hello Qt World
ì
Qt uses..
ì
Qt has..
Qt Basics and
using "cin" & "cout"
ì
:
string ମ QString
:
vector ମ QVector
:
iostream/iostringstream ମ QTextStream
:
ifstream/ofstream ମ QFile & QTextStream
Moving to Qt
:
:
23/01/12
7
23/01/12
: for, if, while, class, ....
For each STL class, we'll be moving to Qt.
We will quickly not need..
8
cout/cin/cerr
ì
Linkage
Replacing cout/cin
:
:
:
ì
cout/cin/cerr are in the std namespace.
Part of the iostream library.
C provides i/o file access: stdout, stdin, stderr
Replace cout/cin with QTextStream's:
Linkage refers to 'share-ability' of items across files:
:
Internal Linkage:
:
Identifier = function or variable.
ì Two .cpp files may independently define the
same identifier.
External Linkage:
ì
ì
ì
Name conflict if two files declare same identifier.
One Definition Rule:
:
23/01/12
9
23/01/12
External Linkage Example
linkage1.cpp
10
extern
linkage1.cpp
linkage2.cpp
linkage2.cpp
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
const int ZERO = 0;
int g_badIdea = 1;
const int ZERO = 0;
int g_badIdea = 1;
const int ZERO = 0;
int g_badIdea = 1;
void foo()
{
cout << "I pity the FOO!" << endl;
};
void foo()
{
cout << "I pity the FOO!" << endl;
};
void foo()
{
cout << "I pity the FOO!" << endl;
};
int main() {
// ...
}
ì
g_badIdea and foo() both...
Create the
variable &
function.
Add extern to change a
into a
linkage2.obj : error LNK2005: "void __cdecl foo(void)" (?foo@@YAXXZ) already defined in linkage1.obj
linkage2.obj : error LNK2005: "int badIdea" (?badIdea@@3HA) already defined in linkage1.obj
23/01/12
#include <iostream>
using namespace std;
11
ì
const int ZERO = 0;
extern int g_badIdea;
extern void foo();
Use variable
& function
defined in
other file.
void bar()
{
foo();
cout << "0: " << ZERO << endl;
cout << "1: " << g_badIdea
<< endl;
};
Only one 'instance' created,
but other file can use it.
23/01/12
12
Static & Linkage Summary
Back to Qt vs cout
Changing External to Internal
ì
:
Add static to a function or variable definition to..
ì
What's wrong with this?
main.cpp
:
Makes it so 2 files can independently declare
variables/functions..
linkage1.cpp
linkage2.cpp
#include <iostream>
using namespace std;
int g_badIdea = 1;
void foo() { };
// ...
#include <iostream>
using namespace std;
static int g_badIdea = 1;
static void foo() { };
// ...
ì
this g_badIdea
and foo() have..
Linkage Summary:
Internal Linkage
External Linkage
Functions
Static Functions
Normal Functions
Variables
Static "Global" Variables
Global Variables
Constants
23/01/12
13
cake.h
#include "cake.h"
#include <QTextStream>
QTextStream cout(stdout);
QTextStream cin(stdin);
class Cake {
public:
void eat();
};
int main()
{
Cake dinner();
dinner.eat();
cout << "Mmmm Good." << endl;
}
#include "cake.h"
#include <QTextStream>
QTextStream cout(stdout);
QTextStream cin(stdin);
cake.cpp
void Cake::eat()
{
cout << "Munch Munch" << endl;
}
23/01/12
14
#ifndef STD2QT_H
#define STD2QT_H
#include <QTextStream>
main.cpp
std2qt.h
Solution: std2qt C-Library
23/01/12
cake.h
#include "std2qt.h"
QTextStream std2qt::cout(stdout);
QTextStream std2qt::cin(stdin);
QTextStream std2qt::cerr(stderr);
int main() {
Cake dinner();
dinner.eat();
cout << "Mmmm Good." << endl;
}
class Cake {
public:
void eat();
};
cake.cpp
std2qt.cpp
namespace std2qt {
extern QTextStream cout;
extern QTextStream cin;
extern QTextStream cerr;
};
#endif
#include "cake.h"
#include "std2qt.h"
using namespace std2qt;
#include "cake.h"
#include "std2qt.h"
using namespace std2qt;
void Cake::eat() {
cout << "Munch Munch" << endl;
}
STD to Qt
Replacement Classes
15
23/01/12
16
Ch1.8
QString
ì
:
:
:
23/01/12
ì
ì
:
ì
ì
:
:
17
Handles file management
Input/output done through..
:
23/01/12
cout << "Your name: " << flush;
Some modifiers are different:
:
hi[0] returns a
QChar, not a
char!
QString name =
Output "flushed" at new line; force with flush:
:
Very similar to
std::string
interface.
See documentation for more.
QFile
Read a line of text:
:
qSetFieldWidth(10) vs setw()
qSetRealNumberPrecision() vs setprecision()
Ex:
cout << left << qSetFieldWidth(10) << "Height: "
<< qSetRealNumberPrecision(2) << 42.11112;
See documentation for more.
18
Ch1.10
QFile
ì
Ch1.9
This slide assumes cin/cout are our custom QTextStream objects!
Supports Unicode
Richer API, easier to use
Interacts well with rest of Qt
#include <QString>
int main()
{
QString hi("Hello");
QString world = "World";
int
len = hi.length();
QChar
qletter = hi[0];
char
ch = qletter.toAscii();
QString both = hi + " " + world;
if (hi == "Hello")
hi = "bye";
}
More on QTextStream
#include <QFile>
int main()
{
// Data to write
QString name = "Dr. Evil";
int favNum = 42;
QTextStream
constructor takes
QFile pointer (&).
// Open the file
QFile writeFile("test.txt");
writeFile.open(QIODevice::WriteOnly);
Ready-Made
Qt Dialogs
// Write to the file through a QTextStream
QTextStream fileTxtStream(&writeFile);
fileTxtStream << "Hello " << name
<< favNum << endl;
fileTxtStream << "The end!\n";
:
// Close the file
writeFile.close();
}
23/01/12
19
23/01/12
20
Ch1.11
Qt Dialogs
#include <QtGui>
const int NO_PARENT = 0;
const char *APP_NAME = "Qt Dialog Demo";
int main(int argc, char *argv[]) {
// Support graphical applications
QApplication app(argc, argv);
Use Qt's GUI features.
// Continued from previous...
// Output
QString message = QString("Nice name, %1!")
.arg(name);
QMessageBox::information(
NO_PARENT, APP_NAME,
message);
Graphical Qt applications
need a QApplication.
// Get a number
int favNum = QInputDialog::getInt(
NO_PARENT, APP_NAME,
"What is your favourite number?");
if (answer == QMessageBox::Yes) {
// Do something here...
}
// More...
return 0;
21
Summary
ì
Qt will largely replace STD library for us.
ì
Linkage:
:
:
ì
:
:
ì
23/01/12
External Linkage: Sharable between files.
Internal Linkage: Not sharable between files.
Use a QTextStream to:
:
Can build up QStrings:
substitute the %1 %2 ...
// Input
int answer = QMessageBox::question(
NO_PARENT, APP_NAME,
"Do you know binary?",
QMessageBox::Yes | QMessageBox::No);
// Get a string
QString name = QInputDialog::getText(
NO_PARENT, APP_NAME,
"What is your name?");
23/01/12
Ch1.11
Qt Dialogs
Replace cin, cout, cerr
Build a string
Write to/read from a file
Qt has ready-made dialogs which are quick to use.
23
23/01/12
}
QtDialogDemo & QtDialogFactorial (Refactor?)
22
© Copyright 2026 Paperzz