GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen...

Preview:

Citation preview

GUIs

Basic Concepts

GUI

• GUI : Graphical User Interface• Window/Frame : a window on the screen• Controls/Widgets : GUI components

QT

• QT – layer of code to enable platform independent applications

QT

• Must add QT += widgets to .pro file– Tells QTCreator to look for those .h files / libraries

QT

• QApplication : class that manages resources, runs event loop– Create BEFORE anything GUI related– exec starts loop

• Last thing main does• returns when all windows closed

QObject

• All GUI components are QObjects

Most Important Widgets

• Display text : Qlabel• Text input : QLineEdit• Button : QPushButton

Making a Window

• Any Qwidget can be a window– Call show method

Styling

• Can modify style through code:

Styling

• Can style with HTML within strings:

Styling

• Can apply style sheets to widgets

Event Handling & Layout

Event Based Programming

• Event : anything that happens– Window resized– Button pressed– Network connection closes

Event Based Programming

• Event : anything that happens– Window resized– Button pressed– Network connection closes

• Event based programs:– Enter infinite loop– Wait for events to happen – respond to them

Notifications

• Callback : – Way to register a function to be called

in certain situation

Notifications

• Callback : – Way to register a function to be called

in certain situation

• Function Pointer : stores address of a function– Type depends on return type and parameters

Function Pointers

• Name of function by itself is memory address• Use to set function pointers:

Define

• Compiler macro– Preprocessor definition that replaces your source code with

something more complex

Slots & Signals

• In QT– Signal : a message that a Widget can send– Slot : a function that can receive a message

• Signals wired to 0+ slots• On event, sends signal to

each listening slot

A Working Button

• Want a button that quits program:

A Working Button

• Want a button that quits program:

• Need to hook a signal from Button

• To slot in application

A Working Button

• Want a button that quits program:

• Need to hook a signal from Button

• To slot in application

Quitting

• Qobject::connect function hooks signal to slot– SIGNAL / SLOT ugly macros• Address of widget• Function to use as SIGNAL / SLOT

Layout

• Cross platform /resizeable GUIs need to be flexible• Place components in layout boxes (sizers)– Components request needed size– Let layout boxes organize components

A Simple QT Layout

• Layout widgets– Invisible boxes that group widgets• QVBoxLayout : Vertical group• QHBoxLayout : Horizontal group

In Code

Make widgetsAdd to layout

Behaviors

• Behavior hookup:

Behavior Errors

• Hookup happens at runtime… watch for errors in Application Output– Stuff inside SLOT / SIGNAL just treated as strings!

Recommended