Qt Signals and Slots. Olivier Goffart. October 2013



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

New Qt APIs for Mobile Development. Mobility I Alex Blasche

Illustration 1: Diagram of program function and data flow

A Deeper Look at Signals and Slots

CORBA Programming with TAOX11. The C++11 CORBA Implementation

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Qt Tips and Tricks. Integrated Computer Solutions Incorporated. Justin Noel and the ICS Team Oct 2010

Object Oriented Software Design II

[PRAKTISCHE ASPEKTE DER INFORMATIK SS 2014]

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

C++FA 5.1 PRACTICE MID-TERM EXAM

The C Programming Language course syllabus associate level

Surface and Volumetric Data Rendering and Visualisation

Help on the Embedded Software Block

C++ INTERVIEW QUESTIONS

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

An Incomplete C++ Primer. University of Wyoming MA 5310

MPLAB Harmony System Service Libraries Help

C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2


C++ Programming Language

Writing a C-based Client/Server

SFINAE Functionality Is Not Arcane

A generic framework for game development

TypeScript for C# developers. Making JavaScript manageable

C Programming Review & Productivity Tools

Getting Started with the Internet Communications Engine

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

Thank you for choosing Huwei E589 4G Mobile WiFi

The GenomeTools Developer s Guide

Grundlagen der Betriebssystemprogrammierung

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

OpenCL Static C++ Kernel Language Extension

vector vec double # in # cl in ude <s ude tdexcept> tdexcept> // std::ou std t_of ::ou _range t_of class class V Vector { ector {

Crash Course in Java

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Building a Python Plugin

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Cross-platform C++ development using Qt

Binary storage of graphs and related data

Extending your Qt Android application using JNI

C++ Support for Abstract Data Types

Object Oriented Software Design II

Support for Embedded Programming in C++11 and C++14

Visual Basic Programming. An Introduction

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Replacing SDL with Qt/QML

Sources: On the Web: Slides will be available on:

Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

N3458: Simple Database Integration in C++11

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Glossary of Object Oriented Terms

INSTALLING AND USING ENTEL PROGRAMMER IN WINDOWS 7 Technical Support Bulletin

Habanero Extreme Scale Software Research Project

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

How To Write Portable Programs In C

Mouse Programming Mouse Interrupts Useful Mouse functions Mouselib.h Mouselib.c

sqlpp11 - An SQL Library Worthy of Modern C++

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Compute Cluster Server Lab 3: Debugging the parallel MPI programs in Microsoft Visual Studio 2005

Development_Setting. Step I: Create an Android Project

Introduction to Distributed Computing using CORBA

Specific Simple Network Management Tools

Proofpoint provides the capability for external users to send secure/encrypted s to EBS-RMSCO employees.

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

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

Yun Shield Quick Start Guide VERSION: 1.0 Version Description Date 1.0 Release 2014-Jul-08 Yun Shield Quick Start Guide 1 / 14

Reading and Writing PCD Files The PCD File Format The Grabber Interface Writing a Custom Grabber PCL :: I/O. Suat Gedikli, Nico Blodow

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

How to: Use Basic C++, Syntax and Operators

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Java Interview Questions and Answers

Chapter 13 Storage classes

ios App for Mobile Website! Documentation!

Instructions For Opening UHA Encrypted

Debugging with TotalView

Exposing C++ functions and classes with Rcpp modules

sys socketcall: Network systems calls on Linux

Habanero Extreme Scale Software Research Project

OMPT: OpenMP Tools Application Programming Interfaces for Performance Analysis

Lenovo Partner Access - Overview

UML - Getting Started EA v4.0

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Orders.java. ArrayList of Drinks as they are ordered. Functions to calculate statistics on Drinks

Java (12 Weeks) Introduction to Java Programming Language

Now you can compile and upload your code supporting highier buffered serial input data. */

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

An Overview of Java. overview-1

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Thank you for choosing HUAWEI Mobile WiFi

Transcription:

Qt Signals and Slots Olivier Goffart October 2013

About Me

About Me QStyleSheetStyle Itemviews Animation Framework QtScript (porting to JSC and V8) QObject, moc QML Debugger Modularisation...

About Me Offering Qt help and services: Visit http://woboq.com C++ Code browser: http://code.woboq.org

Outline 1 History 2 Pointer to member function 3 Lambda functions 4 New syntax in Qt5 5 Under The Hood

Outline 1 History 2 Pointer to member function 3 Lambda functions 4 New syntax in Qt5 5 Under The Hood

Qt 1.41

Qt 1.41

Qt 1.41 qobjectdefs.h

Qt 2, Qt 3 Q PROPERTY No major changes in signals and slot

Qt 4 Thread support QueuedConnection Meta type registration Several major internal changes Added file and line number information in debug mode But still no changes in the syntax

How Does it Work? 1 bool connect ( const QObject * sender, 2 const char * signal, 3 const QObject * receiver, 4 const char * member );

How Does it Work? Compare the signature string to see if the arguments match Use the information provided my the moc to find the index of the signal and of the slot Keep in an internal map which signal is connected to what slots When emitting a signal, QMetaObject::activate is called. It calls qt metacall (generated by moc) with the slot index which call the actual slot

Problems 1 connect ( button, SIGNAL ( clicked ()), 2 this, SLOT ( slotcliked ()));

Problems 1 connect ( button, SIGNAL ( clicked ()), 2 this, SLOT ( slotcliked ())); 3 connect ( socket, SIGNAL ( inforeceived ( const Info &)), 4 this, SLOT ( slotinforeceived ( const MyFramework :: Info &)))

Problems 1 connect ( button, SIGNAL ( clicked ()), 2 this, SLOT ( slotcliked ())); 3 connect ( socket, SIGNAL ( inforeceived ( const Info &)), 4 this, SLOT ( slotinforeceived ( const MyFramework :: Info &))) 6 connect ( button3, SIGNAL ( clicked ()), 7 this, SLOT ( buttonclicked (3)));

Problems 1 connect ( button, SIGNAL ( clicked ()), 2 this, SLOT ( slotcliked ())); 3 connect ( socket, SIGNAL ( inforeceived ( const Info &)), 4 this, SLOT ( slotinforeceived ( const MyFramework :: Info &))) 6 connect ( button3, SIGNAL ( clicked ()), 7 this, SLOT ( buttonclicked (3))); 9 connect ( combobox, SIGNAL ( valuechanged ( int )), 10 settings, SLOT ( updatevalue ( QVariant )));

Problems 1 connect ( button, SIGNAL ( clicked ()), 2 this, SLOT ( slotcliked ())); 3 connect ( socket, SIGNAL ( inforeceived ( const Info &)), 4 this, SLOT ( slotinforeceived ( const MyFramework :: Info &))) 6 connect ( button3, SIGNAL ( clicked ()), 7 this, SLOT ( buttonclicked (3))); 9 connect ( combobox, SIGNAL ( valuechanged ( int )), 10 settings, SLOT ( updatevalue ( QVariant ))); 12 connect ( model, SIGNAL ( modelreset ()), 13 this, SLOT ( onelineslot ()));

Qt 5 syntax 1 connect ( action, SIGNAL ( selected ( QString )), 2 receiver, SLOT ( actionselected ( QString ))); 3 4 connect ( action, & QAction :: selected, 5 receiver, & Receiver :: actionselected ); 6 7 connect ( action, & QAction :: selected, 8 []( const QString & act ) { 9 qdebug () << " Action selected :" << act ; 10 });

Outline 1 History 2 Pointer to member function 3 Lambda functions 4 New syntax in Qt5 5 Under The Hood

Pointer to member 1 struct Point { int x; int y; }; 2 3 int Point ::* coordinate = 0; 4 5 if ( orientation == Qt :: Horizontal ) 6 coordinate = & Point :: x; 7 else if ( orientation == Qt :: Vertical ) 8 coordinate = & Point :: y; 9 10 Point p = /*... */ 11 Point *pp = /*... */ 12 13 if ( coordinate ) 14 pp - >* coordinate = p.* coordinate ;

Pointer to member function 1 struct Point { int x() const ; int y() const ; 2 void setx ( int ); void sety ( int ); }; 3 4 int ( Point ::* getter )() const = 0; 5 void ( Point ::* setter )( int ) = 0; 6 7 if ( orientation == Qt :: Horizontal ) { 8 getter = & Point :: x; 9 setter = & Point :: setx ; 10 } 11 12 Point p = /*... */ 13 Point *pp = /*... */ 14 15 if ( getter && setter ) { 16 int c = (p.* getter )() 17 (pp - >* setter )(c); 18 }

Fun facts 1 int Point ::* coordinate = 0; 2 int ( Point ::* getter )() = 0;

Fun facts 1 int Point ::* coordinate = 0; 2 int ( Point ::* getter )() = 0; 3 qdebug () << sizeof ( coordinate ) << sizeof ( getter );

Fun facts 1 int Point ::* coordinate = 0; 2 int ( Point ::* getter )() = 0; 3 qdebug () << sizeof ( coordinate ) << sizeof ( getter ); 8 16

Fun facts 1 int Point ::* coordinate = 0; 2 int ( Point ::* getter )() = 0; 3 qdebug () << sizeof ( coordinate ) << sizeof ( getter ); 8 16 4 qdebug () << * reinterpret_ cast < int * >(& coordinate );

Fun facts 1 int Point ::* coordinate = 0; 2 int ( Point ::* getter )() = 0; 3 qdebug () << sizeof ( coordinate ) << sizeof ( getter ); 8 16 4 qdebug () << * reinterpret_ cast < int * >(& coordinate ); -1

Not So Fun Facts 1 struct Struct { 2 int foo ( int ); 3 int bar ( int ); 4 int bar ( double ); 5 }; 6 int ( Struct ::* barp1 )( int ) = & Struct :: bar ;

Not So Fun Facts 1 struct Struct { 2 int foo ( int ); 3 int bar ( int ); 4 int bar ( double ); 5 }; 6 int ( Struct ::* barp1 )( int ) = & Struct :: bar ; 7 auto foop = & Struct :: foo ;

Not So Fun Facts 1 struct Struct { 2 int foo ( int ); 3 int bar ( int ); 4 int bar ( double ); 5 }; 6 int ( Struct ::* barp1 )( int ) = & Struct :: bar ; 7 auto foop = & Struct :: foo ; 8 // decltype ( foop ): int ( Struct ::*)( int )

Not So Fun Facts 1 struct Struct { 2 int foo ( int ); 3 int bar ( int ); 4 int bar ( double ); 5 }; 6 int ( Struct ::* barp1 )( int ) = & Struct :: bar ; 7 auto foop = & Struct :: foo ; 8 // decltype ( foop ): int ( Struct ::*)( int ) 10 auto barp2 = & Struct :: bar ;

Not So Fun Facts 1 struct Struct { 2 int foo ( int ); 3 int bar ( int ); 4 int bar ( double ); 5 }; 6 int ( Struct ::* barp1 )( int ) = & Struct :: bar ; 7 auto foop = & Struct :: foo ; 8 // decltype ( foop ): int ( Struct ::*)( int ) 10 auto barp2 = & Struct :: bar ; error: variable barp2 with type auto has incompatible initializer of type <overloaded function type> error: unable to deduce auto from & Struct::bar

Outline 1 History 2 Pointer to member function 3 Lambda functions 4 New syntax in Qt5 5 Under The Hood

Lambda λ

Lambda [foo] (int a) -> int { return a + foo; } Capture: Variables that you capture Parametter list: The perametters of the function Return type (optional) Function body

Lambda [foo] (int a) -> int { return a + foo; } struct { double foo; int operator()(int a) { return a + foo; } }

Lambda capture 1 int a = 1, b = 2, c = 3; 2 3 // a by value, b by reference 4 auto f1 = [a, &b ]() { b = a; }; 5 6 // everything by reference 7 auto f2 = [&]() { b = a; }; 8 9 // everything by value 10 auto f3 = [=]() { return a + c; }; 11 12 // everything by value, b by reference 13 auto f4 = [=,&b]() { b = a + c; };

Examples 1 connect ( button3, & QBushButton :: clicked, 2 [=] { this -> buttonclicked (3); }); 3 4 connect ( model, & QAbstractItemModel :: modelreset, 5 [] { qdebug () << " model has been reset "; });

Outline 1 History 2 Pointer to member function 3 Lambda functions 4 New syntax in Qt5 5 Under The Hood

New connect Overloads 1 QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *slot, Qt::ConnectionType type ) 2 QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction slot, Qt::ConnectionType type ) 3 QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor method ) 4 QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor method, Qt::ConnectionType type ) (since Qt 5.2)

New connect Overloads 1 QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *slot, Qt::ConnectionType type ) 2 QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction slot, Qt::ConnectionType type ) 3 QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor method ) 4 QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor method, Qt::ConnectionType type ) (since Qt 5.2)

Context There is no receiver when connecting to a lambda. Receiver can be used for: 1 Thread affinity (QueuedConnection) 2 Automatic disconnection when the receiver is destroyed 3 sender() In Qt 5.2 you can use a context with your lambda function 1 connect ( button3, & Button :: clicked, 2 bar, [=]{ bar -> buttonclicked (3); });

Remember QSignalMapper? 1 for ( int i = 0; i < texts. size (); ++i) { 2 QPushButton * button = new QPushButton ( texts [ i], 3 this ); 4 5 // A C ++11 lambda 6 connect ( button, & QPushButton :: clicked, 7 [=]{ this -> select ( texts [i]) }); 8 9 // OR, without C ++11, using tr1 / boost bind 10 connect ( button, & QPushButton :: clicked, 11 bind (& MyWidget :: select, this, texts [i ])); 12 }

Qt 5 syntax Design Goals Detect as many error as possible at compile time Be easy and intuitive Do not require users to understand templates or function pointers Do not require C++11

Qt 5 syntax History Research started in august 2009 ( Qt 4.6) First trial: 1 connect ( QSignal (s, & SenderObject :: signal1 ), 2 QSlot (r1, & ReceiverObject :: slot1 ));

Protected Signals In Qt4 (and before) 1 # define signals protected 37 connect ( MyObject, & QObject :: destroyed, //... main.cc:37:22: error: destroyed is a protected member of QObject

Protected Signals Can we change? 1 # define signals public

Protected Signals Can we change? 1 # define signals public Two problems Binary compatibility Everybody can emit a signal from another object

Typing the type name Is there a way to avoid typing the type of the object? 1 connect ( QSIGNAL ( button, clicked ), 2 QSLOT ( receiver, buttonclicked ));

Typing the type name Is there a way to avoid typing the type of the object? 1 connect ( QSIGNAL ( button, clicked ), 2 QSLOT ( receiver, buttonclicked )); 10 // C ++11 Only 11 # define QSIGNAL (OBJ, FUNC ) OBJ, [&](){ \ 12 typedef std :: remove_reference < decltype (*( OBJ )) >:: type Type ; \ 13 return & Type :: FUNC ; }()

Overloads 1 2 class Obj : public QObject { 3 Q_ OBJECT 4 signals : 5 void valuechanged ( int ); 6 void valuechanged ( const QString &); 7 }; 8 9 QObject :: connect (obj, & Obj :: valuechanged, []{});

Overloads 1 2 class Obj : public QObject { 3 Q_ OBJECT 4 signals : 5 void valuechanged ( int ); 6 void valuechanged ( const QString &); 7 }; 8 9 QObject :: connect (obj, & Obj :: valuechanged, []{}); error: no matching function for call to QObject::connect(Obj* const, <unresolved overloaded function type>, lambda0)

Overloads 1 2 class Obj : public QObject { 3 Q_ OBJECT 4 signals : 5 void valuechanged ( int ); 6 void valuechanged ( const QString &); 7 }; 8 9 QObject :: connect (obj, & Obj :: valuechanged, []{}); error: no matching function for call to QObject::connect(Obj* const, <unresolved overloaded function type>, lambda0) 1 QObject :: connect (obj, 2 static_cast < void ( Obj ::*)( int ) >(& Obj :: valuechanged ), 3 []{});

Overloads Avoid overloading signals!

Outline 1 History 2 Pointer to member function 3 Lambda functions 4 New syntax in Qt5 5 Under The Hood

Outline 5 Under The Hood Moc Connections Emiting a Signal New Syntax

Outline 5 Under The Hood Moc Connections Emiting a Signal New Syntax

Connections

Outline 5 Under The Hood Moc Connections Emiting a Signal New Syntax

Outline 5 Under The Hood Moc Connections Emiting a Signal New Syntax

New Syntax

Summary Compile time checks Not problems in arguments with namespaces or typedef Automatic type converstions

Summary Compile time checks Not problems in arguments with namespaces or typedef Automatic type converstions With C++11 you benefit from No 6 arguments limit Better error messages static assert Lambda functions

The END Questions olivier@woboq.com visit http://woboq.com