[PRAKTISCHE ASPEKTE DER INFORMATIK SS 2014]

Similar documents
Surface and Volumetric Data Rendering and Visualisation

Qt in Education. The Qt object model and the signal slot concept

Andreas Burghart 6 October 2014 v1.0

New Qt APIs for Mobile Development. Mobility I Alex Blasche

Cross-platform C++ development using Qt

The Most Popular UI/Apps Framework For IVI on Linux

BogDan Vatra and Andy Gryc. Qt on Android: Is it right for you?

Programmers rejoice: QML makes business people understand. Qt Developer Days 2014 Hinrich Specht 2. September 2014 Folie 1

Lazy OpenCV installation and use with Visual Studio

Crash course to. Qt Quick Game Programming

Best Practices in Qt Quick/QML. Langston Ball Senior Qt Developer ICS, Inc.

Qt Signals and Slots. Olivier Goffart. October 2013

Brent A. Perdue. July 15, 2009

[PRAKTISCHE ASPEKTE DER INFORMATIK WS 13/14]

Fireworks 3 Animation and Rollovers

CMSC 427 Computer Graphics Programming Assignment 1: Introduction to OpenGL and C++ Due Date: 11:59 PM, Sept. 15, 2015

Beginning Nokia Apps. Development. Qt and HTIVIL5 for Symbian and MeeGo. Ray Rischpater. Apress. Daniel Zucker

Specialized Android APP Development Program with Java (SAADPJ) Duration 2 months

Give a Push to Your Qt Application with Qt Cloud Services and WebSockets

DEVELOPMENT OF AN ANALYSIS AND REPORTING TOOL FOR ORACLE FORMS SOURCE CODES

Introduction to GUI programming in Python. Alice Invernizzi

Code::Blocks Student Manual

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

QNX SDK for Apps and Media 1.1. Qt Developer's Guide

CSC230 Getting Starting in C. Tyler Bletsch

Building a Python Plugin

Mobile Development with Qt

LAB4 Making Classes and Objects

A Deeper Look at Signals and Slots

Running your first Linux Program

BlackBerry 10. App Development. Learn. A Cascades-Driven Approach. Build cutting-edge BlackBerry 10 apps with Qt, C++, and the Cascades UI Framework

Creating Your Teacher Website using WEEBLY.COM

#include <Gamer.h> Gamer gamer; void setup() { gamer.begin(); } void loop() {

Introduction CMake Language Specific cases. CMake Tutorial. How to setup your C/C++ projects? Luis Díaz Más.

A generic framework for game development

Part 1 Foundations of object orientation

How to develop your own app

Network Programming in Qt. TCP/IP Protocol Stack

David Boddie. PyCon UK 2007, Birmingham

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Drawing Text with SDL

QML and JavaScript for Native App Development

CHAPTER 26 - SHOPPING CART

Backing up with Windows 7

Qt on i.mx6. Hands-on Instructions. Copyright 2012 Digia Plc.

Randy Hyde s Win32 Assembly Language Tutorials (Featuring HOWL) #4: Radio Buttons

Creative Guidelines for s

Discover the framework and make your first steps with it.

C++ Programming with Qt 3

IOIO for Android Beginners Guide Introduction

caqtdm Tutorial Jim Stevens APS Controls Group October 9, 2014

Lecture 1 Introduction to Android

Informatica e Sistemi in Tempo Reale

Please select one of the topics below.

APPLICATIONS OF LINUX-BASED QT-CUDA PARALLEL ARCHITECTURE

Continuous Integration

Configuring the JEvents Component

ANIMATED HEADER IMAGE WITH IMAGE HEADER SLIDESHOW (FL_HEADER_SLIDE)

We automatically generate the HTML for this as seen below. Provide the above components for the teaser.txt file.

Qt/Embedded Whitepaper. Trolltech

LESSON 7: IMPORTING AND VECTORIZING A BITMAP IMAGE

Scoop Hosted Websites. USER MANUAL PART 4: Advanced Features. Phone: scoop@scoopdigital.com.au Website: scoopdigital.com.

R&W Recipe Designer Recipe Management for SIMATIC WinCC. Version 4.0 SP1 Hotfix 2. Recipe Designer CS

Embedded Software Development with MPS

Creating a 2D Game Engine for Android OS. Introduction

Catalog Creator by On-site Custom Software

Visual Basic Programming. An Introduction

SelectSurvey.NET Developers Manual

TCP/IP Networking, Part 2: Web-Based Control

Application Note. Building a Website Using Dreamweaver without Programming. Nan Xia. MSU ECE 480 Team 5

Piktochart 101 Create your first infographic in 15 minutes

IBM BPM V8.5 Standard Consistent Document Managment

OpenCV on Android Platforms

Extending your Qt Android application using JNI

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Computing Concepts with Java Essentials

Version Control with. Ben Morgan

HOW TO BUILD AND CUSTOMIZE YOUR FACEBOOK FAN PAGE (ON THE CHEAP)

Introduction to Tizen SDK Alpha. Taiho Choi Samsung Electronics

Writing standalone Qt & Python applications for Android

Xtreeme Search Engine Studio Help Xtreeme

Introduction to iphone Development

Microsoft Excel 2007 Finding Objects and How To

An Overview of Java. overview-1

Learning Remote Control Framework ADD-ON for LabVIEW

3. Programming the STM32F4-Discovery

GUI application set up using QT designer. Sana Siddique. Team 5

Fireworks CS4 Tutorial Part 1: Intro

OUTLOOK WEB APP 2013 ESSENTIAL SKILLS

EE8205: Embedded Computer System Electrical and Computer Engineering, Ryerson University. Multitasking ARM-Applications with uvision and RTX

Adobe Illustrator CS6. Illustrating Innovative Web Design

Transcription:

2014 Institut für Computergraphik, TU Braunschweig Pablo Bauszat [PRAKTISCHE ASPEKTE DER INFORMATIK SS 2014] All elemental steps that will get you started for your new life as a computer science programmer.

What is Qt? Week E GUIs with Qt Pros and Cons What does Qt do with my code? Examples Qt Designer,.ui and.moc files Signals and slots MOC and UIC Make, CMake, QMake Week E GUIs with Qt This week we ll talk a little bit about Qt and its capabilities to create nice GUIs. As you might know, Qt comes with a lot of extra feature and is far more than just another GUI library. A slight drawback is that Qt needs to influence your whole build process because it introduces non C++-conform syntax in your code. Before the actual compiler starts parsing, Qt itself parses your files and automatically creates C++ conform code. 2 PADI Praktische Aspekte der Informatik

What is Qt? Qt is a cross-platform application and UI framework - http://qt-project.org/ Comes with a ton of extras Has its own build system (qmake) Now also packed in a single IDE (Qt Creator) Qt is very complex, but has a great documentation Make use of it! Online (use google) or offline (Qt Assistant) It is good to know that Qt provides everything for your programming needs: Its own build system (qmake), its own IDE (Qt Creator) and its own UI Designer Program (Qt Designer). It is even better to know that you do not have to use them if you don t like to. You can use Cmake and your own favorite IDE/Editor if you prefer. As there are a lot of resources to learn Qt, it may be difficult to pick one good tutorial to get started. For the exercise material, I combined several tutorials that can be useful for you, too: Learn Qt - http://thelins.se/learnqt/tutorial/ Qt Getting started - http://qt-project.org/doc/qt-5.1/qtdoc/gettingstarted.html (Für Qt 4) Zetcode Qt4 tutorial - http://zetcode.com/gui/qt4/ 3 PADI Praktische Aspekte der Informatik

Qt is But Qt Pros and cons Powerful Big Filled with useful extras Has excellent documentation Extends C++ standard You might not need everything 4 PADI Praktische Aspekte der Informatik

Signals and slots Qt So what s different? Classes can communicate with SIGNALS and SLOTS, these can be queried/connected at runtime Code Generators Qt-specific stuff is translated to standard C++ UIC: gui-xml to C++ headers MOC: Qt-enhanced C++ (usually headers) to C++ RCC: Resources into binaries Qt introduces several new concepts to standard C++. Communication inside and between Qt-Classes is achieved with a signal and slot mechanism. For Qt 4 you can find a good introduction here http://qt-project.org/doc/qt-4.8/signalsandslots.html Careful! The new Qt 5 version changed the syntax of signals and slots (http://qtproject.org/wiki/new_signal_slot_syntax). 5 PADI Praktische Aspekte der Informatik

Counter.h Counter.cpp class Counter : public QObject { Q_OBJECT // Important!! private: int m_value; public: Counter() { m_value = 0; } int value() const { return m_value; } public slots: // Notice the new C++ keyword void setvalue(int value); signals: // Notice the new C++ keyword // This function is not implemented in this class! void valuechanged(int newvalue); not implem }; void Counter::setValue(int value) { if (value!= m_value) { m_value = value; emit valuechanged(value); } } // Emit a signal 6 PADI Praktische Aspekte der Informatik

Image source: http://thelins.se/learnqt/wp-content/uploads/qt-buildsystem.png 7 PADI Praktische Aspekte der Informatik

00_qt Basic Makefile to get started with Qt INCPATH = -I/usr/include/qt4/QtGui -I/usr/include/qt4 LIBPATH = -L/usr/lib/ OPTIONS = -lqtcore -lqtgui CCFLAGS = -Wall -g EXE=qt_app $(EXE): main.o g++ $(LIBPATH) $(OPTIONS) $^ -o $@ main.o: main.cpp g++ $(INCPATH) $(CCFLAGS) -c $< clean: rm -f *.o *~ $(EXE) In order to compile your first Qt application, you just need to include the Qt headers and link to the needed libraries. Notice that the libraries depend on the classes you use (often times you need QtCore, QtGui and QtOpenGL). Also note that the libraries might change between major Qt version (e.g. for Qt 5 you need to also link against QtWidgets). 8 PADI Praktische Aspekte der Informatik

00_qt main.cpp #include <QApplication> #include <QWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.resize(250, 150); window.setwindowtitle("simple example"); window.show(); } return app.exec(); It s important to have a QApplication object. After you set up your application in the main method, you call QApplication::exec() to enter the Qt main loop. 9 PADI Praktische Aspekte der Informatik

01_moc Communicate.h class Communicate : public QWidget { Q_OBJECT }; public: Communicate(QWidget *parent = 0); public slots: void OnPlus(); void OnMinus(); private: QLabel *label_number; Please note that no special syntax is used in.cpp file If you are implementing your own Qt Class, e.g. if you derive a class from QWidget and use your own slots/signals, you have to include the Q_Object keyword right after class definition. The official Qt website says: The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system. Then you can use the extra keywords slots, signals and emit. Before you call the C++ compiler, you have to tell the Qt Meta Object Compiler (MOC) to convert your code into standard C++ code. 10 PADI Praktische Aspekte der Informatik

Image source: http://thelins.se/learnqt/wp-content/uploads/qt-buildsystem.png When deriving a class from QObject, you have to run the MOC to get some meta information about the class. MOC parses the header file and create C++ code that lets you query information about the object, i.e., its name, the position and signature of slots. Typically, these files are named or moc_originalname.cpp originalname.moc.cpp originalname.moc.cxx They have to be compiled and linked like any other.cpp file. 11 PADI Praktische Aspekte der Informatik

01_moc Makefile $(EXE): main.o moc_communicate.o communicate.o g++ $(LIBPATH) $(OPTIONS) $^ -o $@ main.o: main.cpp g++ $(INCPATH) $(CCFLAGS) -c $< moc_communicate.o: moc_communicate.cpp g++ $(INCPATH) $(CCFLAGS) -c moc_communicate.cpp moc_communicate.cpp: communicate.h moc $(INCPATH) $< -o $@ communicate.o: communicate.cpp g++ $(INCPATH) $(CCFLAGS) -c communicate.cpp clean: rm -f *.o *~ $(EXE) moc_*.cpp This is the modified Makefile, the biggest difference is the invocation of MOC to create the file moc_communicate.cpp. Please note that both moc_communicate.o and communicate.o have to be compiled and linked. 12 PADI Praktische Aspekte der Informatik

01_moc generated moc file static const uint qt_meta_data_communicate[] = { //content: 4, // revision 0, // classname 0, 0, // classinfo 2, 14, // methods 0, 0, // properties 0, 0, // enums/sets 0, 0, // constructors 0, // flags 0, // signalcount // slots: signature, parameters, type, tag, flags 13, 12, 12, 12, 0x0a, 22, 12, 12, 12, 0x0a, 0 // eod }; static const char qt_meta_stringdata_communicate[] = { "Communicate\0\0OnPlus()\0OnMinus()\0" }; This is the content of a.moc-file. The file is changed every time you modify and moc the source header or.cpp file and therefore, it is a waste of time to edit the.moc-file by hand. As a hint for moc-files: Simply acknowledge their existence, do not forget to link them but never take any closer look at them 13 PADI Praktische Aspekte der Informatik

Qt Designing your GUI Graphical Interface to create your GUI Easy to use Qt Designer Drag-and-drop GUI elements together Save.ui file (xml-based) The first step in processing a Qt application is usually to start the Qt designer and create your own user-interface layout with a few clicks. If you already have experience with GUI layouts it should be quite self-explanatory. If not, use the documentation. Your interface is stored in special Qt files, the.ui files. 14 PADI Praktische Aspekte der Informatik

Hand-coded GUI setwindowtitle("communicate"); int WIDTH = 350; int HEIGHT = 190; resize(width, HEIGHT); QPushButton *plus = new QPushButton("+", this); plus->setgeometry(50, 40, 75, 30); QPushButton *minus = new QPushButton("-", this); minus->setgeometry(50, 100, 75, 30); label_number = new QLabel("0", this); label_number->setgeometry(190, 80, 20, 30); You can create the contents and (very simplistic) layouts of a GUI by hand. But don t do it! Be more productive by using an IDE! 15 PADI Praktische Aspekte der Informatik

Image source: http://thelins.se/learnqt/wp-content/uploads/qt-buildsystem.png 16 PADI Praktische Aspekte der Informatik

WYSIWYG editor (Qt Designer) Create your GUIs using a graphical editor, the Qt Designer. The designer is already integrated in the Qt Creator and both can be used comfortable from a common IDE. 17 PADI Praktische Aspekte der Informatik

Save as.ui (xml-based) <?xml version="1.0" encoding="utf-8"?> <ui version="4.0"> <class>communicateclassname</class> <widget class="qwidget" name="communicateclassname"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>350</width> <height>190</height> </rect> </property> <property name="windowtitle"> <string>communicate</string> </property> <widget class="qpushbutton" name="plus"> <property name="geometry"> <rect> <x>50</x> <y>40</y> <width>75</width> <height>30</height> [ ] This is the content of a.ui-file. Similar to the.moc files, do not manually change the file, but use the Qt Designer for it. 18 PADI Praktische Aspekte der Informatik

UIC :.ui to.h Convert your GUI into a proper C++ class uic communicate.ui -o ui_community.h Inherit from the newly created class Use the object names of the. ui file as actual class/function names 19 PADI Praktische Aspekte der Informatik

Inherit and call setupui communicate.h #include "ui_communicate.h" [ ] class Communicate : public QWidget, private Ui::CommunicateClassName [ ] communicate.cpp [ ] Ui::CommunicateClassName Communicate::Communicate(QWidget *parent) : QWidget(parent) { setupui(this); // This sets up GUI, always call it first! connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus())); connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus())); [ ] } To use a UI, you derive from the created UI class and then setup the UI using the setupui method passing the instance in the constructor. Make sure, that you always call the method before you use the UI components. Alternatively, you can make your class store a member of the UI-class instead of deriving from it. You still need to setup the member in the constructor. 20 PADI Praktische Aspekte der Informatik

Makefile $(EXE): main.o moc_communicate.o communicate.o g++ $(LIBPATH) $(OPTIONS) $^ -o $@ main.o: main.cpp ui_communicate.h g++ $(INCPATH) $(CCFLAGS) -c $< moc_communicate.o: moc_communicate.cpp g++ $(INCPATH) $(CCFLAGS) -c moc_communicate.cpp moc_communicate.cpp: communicate.h ui_communicate.h moc $(INCPATH) $< -o $@ communicate.o: communicate.cpp ui_communicate.h g++ $(INCPATH) $(CCFLAGS) -c communicate.cpp ui_communicate.h: communicate.ui uic $< -o $@ Of course, the UIC call has to be integrated into the Makefile. More infos on the MOC and UIC can be found (and should be read) at: http://qt-project.org/doc/qt-4.8/moc.html http://qt-project.org/doc/qt-4.8/uic.html 21 PADI Praktische Aspekte der Informatik

Qt Building your application Usual way: qmake (using Qt Creator) Alternative - CMake Keywords QT4_WRAP_UI(), QT4_WRAP_CPP() Avoid Makefiles Not recommended to call MOC and UIC manually 22 PADI Praktische Aspekte der Informatik

03_cmake - CMakeLists.txt PROJECT(QtApp) [ ] FIND_PACKAGE(Qt4 REQUIRED) INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR} ${QT_INCLUDE_DIR}/QtCore ${QT_INCLUDE_DIR}/QtGui ${QtApp_SOURCE_DIR} ) LINK_DIRECTORIES(${QT_LINK_DIRECTORIES}) include(${qt_use_file}) [ ] QT4_WRAP_UI(app_UIS_H ${app_uis}) QT4_WRAP_CPP(app_MOC_SRCS ${app_moc_hdrs}) INCLUDE_DIRECTORIES (${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(qt4_app ${app_srcs} ${app_uis_h} ${app_moc_srcs}) TARGET_LINK_LIBRARIES(qt4_app ${QT_LIBRARIES}) 23 PADI Praktische Aspekte der Informatik

04_qmake Use qmake from the terminal: 1. qmake -project 2. qmake 3. make 24 PADI Praktische Aspekte der Informatik

05_advanced Play around with application Try changing SIGNAL / SLOT connections Modify the *.ui file Try building it with qmake Extend the application See task sheet for some ideas 25 PADI Praktische Aspekte der Informatik

Assignment E: GUIs with Qt Task 1: Clone Brush [50 points] First of all, build the example application (05_advanced) and make it run on your machine. Then proceed and add two new features: Extend the program in a way that you do have two images instead of one (so you need to have two instances of DrawingCanvas. Add the functionality that you can select a region in one of the two images (e.g., by clicking CTRL+Left Mouse Button) and then copy pixels from this spot to the other Drawing Canvas. Two Drawing Canvas Instances. The clone brush is used to copy content into the left image. 26 PADI Praktische Aspekte der Informatik

Task 2: Color Triangle [50 points] Take a look at the class QtColorTriangle class in the folder (06_qtcolortriangle). You can find more details on the class interface here: http://doc.qt.nokia.com/solutions/4/qtcolortriangle/qtcolortriangle. html Create a project file in the Qt Creator for the files in the folder and compile the simple demo program. You should see the color triangle window. In the next step, try to build a dynamic library that includes the QtColorTriangle class. Afterwards add the widget to your application and use the color wheel to pick colors other than blue or red. Also see how you can import the color wheel into your Qt Designer environment. http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html 27 PADI Praktische Aspekte der Informatik