Sven Johannsen C++ User Group Aachen 01/08/15

Slides:



Advertisements
Similar presentations
QT UI Widgets and Layout_1F
Advertisements

QT UI Widgets and Layout_2S
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Object Oriented Programming with Java
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Overview  Miscellaneous Utility Functions  Return Type Deduction  Generic Lambdas  Generalised Lambda Captures  C++14 Literals  constexpr Functions.
Rapid GUI Programming with Python and Qt
What's new in Microsoft Visual C Preview
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Lecture 4: Embedded Application Framework Qt Tutorial Cheng-Liang (Paul) Hsieh ECE 424 Embedded Systems Design.
QT GUI Programming CS340 – Software Design © 2009 – Jason Leigh University of Illinois at Chicago.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
QT Intro. 김기형
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Oct ROOT 2002, CERN, Geneva Qt-Based Implementation of Low Level ROOT Graphical Layer By V.Fine.
Qt Igor November 8, 2002 Friday’s HENP Group Meeting.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Blanchette and Summerfield, Ch. 2
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Using Qt for GUI development Brad Whitlock March 2002.
Adding a New Option to the Framework. Introduction This is intended as a step by step guide to adding a new action to the menu or toolbar. The order of.
Dale Roberts Introduction to Visual Programming Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
-1- National Alliance for Medical Image Computing First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010.
Confidential © 2008 Teleca AB Creating Custom Widgets Author: Patricia Jiang Date: July 29, 2010 Version: 1.0 Teleca Chengdu.
QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.
GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen Controls/Widgets : GUI components.
QT – Introduction C++ GUI Programming with Qt 4
QT – Windows, menus, and such C++ GUI Programming with Qt 4 Qt 4.5 Reference Documentation Blanchette and Summerfield, Ch. 3.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
C++ Functions A bit of review (things we’ve covered so far)
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
July FLTK The Fast Light Toolkit • A C++ graphical user interface toolkit • Can be used under X, Windows, MacOS • Supports OpenGL • Provides: – Interactive.
1. 2 Qt Toolkit ● C++ toolkit for cross-platform GUI application development – GUI functions (GUI = graphical user interface) – Data access components,
Motivation for Generic Programming in C++
C++ Lesson 1.
Mark Redekopp David Kempe
Chapter 6 CS 3370 – C++ Functions.
Data types Data types Basic types
CMPE Data Structures and Algorithms in C++ December 7 Class Meeting
QT graphical user interface framework
Pointers and References
Reserved Words.
Qt Programming.
C++ Functions, Classes, and Templates
null, true, and false are also reserved.
Initialization List.
Built-In (a.k.a. Native) Types in C++
Custom Widgets & Events
Constructors and Other Tools
Introduction to Programming
Classes.
Getting Started with Milestone 2
2. Second Step for Learning C++ Programming • Data Type • Char • Float
CIS 199 Final Review.
A multi-platform GUI-building program
Building Blocks of C Programming Language
A multi-platform GUI-building program
SPL – PS1 Introduction to C++.
Presentation transcript:

Sven Johannsen C++ User Group Aachen 01/08/15 Signals and Lambdas Sven Johannsen C++ User Group Aachen 01/08/15

Content Qt 4 Signal and Slot Introduction The connect() Problem Qt5 New Syntax C++ Lambdas as Qt Slots Simple Webbrowser New features - new problems

Qt Signal and Slot Introduction MyWidget Header: class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); private slots: void helloW(); };

Qt Signal and Slot Introduction MyWidget constructor: MyWidget::MyWidget(QWidget *parent): QMainWindow(parent) { auto* button = new QPushButton("Klick mich", this); button->setGeometry(20,20,140,140); connect(button, SIGNAL(clicked()), this, SLOT(helloW())); }

Qt Signal and Slot Introduction helloW Slot: void MyWidget::helloW() { QMessageBox::information(this, nullptr, "Hallo World"); }

Qt4 The connect() Problem MACROs Not compile time save! connect(button, SIGNAL(clicked()), this, SLOT(helloW())); MACROs Not compile time save! Not a function call, but function call syntax Slots has to be a widgets member functions A lot of text for simple actions

Qt5 New Syntax No MACROs, pure C++ Type safe connect(button,&QPushButton::clicked,this,&MainWindow::hello); No MACROs, pure C++ Type safe Support for all(?) kinds of callable types Member functions (no need for slots) Non-member functions Functors, std::function, lambda expressions …

Qt5 New Syntax goto source_code;

C++11 Lambda expressions [ capture ] ( parameter) -> return_type { body; } (); + mutable, throw mandatory: [ ] => lambda-introducer { } => Body Shortest Lambda: []{} []{}(); // Calls an empty lambda

C++11 Lambda expressions Examples (new STL algorithms): vector<int> v{ 13, 15, 19, 3 }; bool b1 = all_of(begin(v), end(v), [](int i){ return i % 2; }); // true bool b2 = any_of(begin(v), end(v), [](int i){ return i < 0; }); // false bool b3 = none_of(begin(v), end(v), [](int i){ return i < 0; }); // true

Qt5 C++ Lambdas as Qt Slots connect(button, &QPushButton::clicked, [] { QMessageBox::information(nullptr, nullptr, "Hallo Lambda"); });

C++11 Lambda expressions Captures [] Capture nothing – only lambda-introducer [=],[&]Capture everything as copy or reference [bar] Capture bar as copy and nothing else [&foo] Capture foo as reference and nothing else [this] Capture this pointer

C++ Lambdas as Qt Slots capture “this” connect(button, &QPushButton::clicked, [this]{ QMessageBox::information(this, nullptr, "Hallo Lambda"); });

C++ Lambdas as Qt Slots parameters connect(webView, &QWebView::titleChanged,[this] (const QString& title){ setWindowTitle(title); });

Qt5 C++ Lambdas as Qt Slots goto source_code; // lambdas and simplebrowser

Simple Webbrowser

Connect: new features - new problems Qt5 Connect: new features - new problems Disconnect Overloads (Default argument in slots are not supported)

Disconnect Disconnect, if the life time is not synchronized MainWindow::~MainWindow() { .... connect(button, &QPushButton::clicked, this, &MainWindow::world); } disconnect(button, &QPushButton::clicked, this, &MainWindow::world);

Disconnect Disconnect, if the life time is not synchronized QMetaObject::Connection world_connection_; MainWindow::~MainWindow() { . . . world_connection_ = connect(button, &QPushButton::clicked, this, &MainWindow::world); } { disconnect(world_connection_);

Overloads Overloaded signals or slots QSpinbox: 2 signals with the same name void valueChanged(int i) valueChanged(const QString & text)

Overloads connect(spin, SIGNAL(valueChanged(int)), label, SLOT(setNum(int))); connect(spin, &QSpinBox::valueChanged, label, &QLabel::setNum); connect(spin, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), label, static_cast<void(QLabel::*)(int)>(&QLabel::setNum));

Disconnect & Overloads goto source_code;

Questions?

simplebrowser.pro QT += core gui webkitwidgets webkit widgets win32-g++: QMAKE_CXXFLAGS += -std=c++11 TARGET = simplebrowser TEMPLATE = app SOURCES += main.cpp HEADERS += simplebrowser.h

main.cpp #include <QApplication> #include "simplebrowser.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

simplebrowser.h #pragma once #include <QMainWindow> #include <QLayout> #include <QLineEdit> #include <QPushButton> #include <QtWebKitWidgets/QWebView> struct MainWindow : QMainWindow { MainWindow() auto central = new QWidget(); auto webView = new QWebView(this); auto urlLineEdit = new QLineEdit(this); auto layout = new QVBoxLayout(central); auto controls = new QHBoxLayout(); auto back = new QPushButton("Back", this); auto forward = new QPushButton("Forward", this); auto stop = new QPushButton("Stop", this); auto reload = new QPushButton("Reload", this); controls->addWidget(back); controls->addWidget(forward); controls->addWidget(urlLineEdit); controls->addWidget(stop); controls->addWidget(reload); layout->addLayout(controls); layout->addWidget(webView); central->setLayout(layout); setCentralWidget(central); connect(back, &QPushButton::clicked, [webView] { webView->back(); }); connect(forward, &QPushButton::clicked, [webView] { webView->forward(); }); connect(stop, &QPushButton::clicked, [webView] { webView->stop(); }); connect(reload, &QPushButton::clicked, [webView] { webView->reload(); }); connect(urlLineEdit, &QLineEdit::returnPressed, [urlLineEdit, webView] { webView->load(QUrl(urlLineEdit->text())); }); connect(webView, &QWebView::urlChanged, [urlLineEdit](const QUrl& url) { urlLineEdit->setText(url.toString()); }); connect(webView, &QWebView::titleChanged, [this](const QString& title) { setWindowTitle("Simple webbrowser : " + title); }); emit webView->titleChanged(""); //webView->load(QUrl("http://sven-johannsen.de")); webView->load(QUrl("http://127.0.0.1:8080")); webView->show(); } };