New Signals and Slots Features in Qt 5: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Don't #include the module prefix)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
This small (cross platform) application demonstrates a few new features that come with Qt 5: the ability to connect to C++ functor objects, non member outer class functions and c++11 lambdas.


This small app (cross platform) demonstrates a few new features come with qt5. thats to ability connect c++ functor objects, non member outer class functions and c+''11 lambda functions. main.cpp:
main.cpp:
<code>
<code>
#include "test.h"
#include "test.h"


void outer_func(){std::printf("this is a test by outer class func;)");}
void outer_func() { std::printf("this is a test by outer class func;)"); }
void functor::operator() (bool) {std::printf("this is a test by functor;)");}
void functor::operator() (bool) { std::printf("this is a test by functor;)"); }


int main(int argc,char* argv[]){
int main(int argc,char* argv[]) {
QApplication app(argc, argv);
    QApplication app(argc, argv);
Ui_Form ui;
    Ui_Form ui;
QWidget* form=new QWidget();
    QWidget* form=new QWidget();
ui.setupUi(form);
    ui.setupUi(form);
form->show();
    form->show();
return app.exec();
    return app.exec();
}
}
</code>
</code>
test.h
test.h
<code>
<code>
#ifndef TEST_H
#ifndef TEST_H
#define TEST_H
#define TEST_H


#include <QVariant>
#include <QVariant>
#include <QAction>
#include <QAction>
#include <QApplication>
#include <QApplication>
#include <QButtonGroup>
#include <QButtonGroup>
#include <QHeaderView>
#include <QHeaderView>
#include <QPushButton>
#include <QPushButton>
#include <QWidget>
#include <QWidget>
#include <cstdio>
#include <cstdio>


void outer_func();
void outer_func();
class functor{public:void operator() (bool);};


functor a_functor; // create an instance of functor
class functor
{
public:
    void operator() (bool);
};


class Ui_Form{
functor a_functor; // create an instance of functor


public:
class Ui_Form
QPushButton *pushButton;
{
void setupUi(QWidget *Form){
public:
    QPushButton *pushButton;
    void setupUi(QWidget *Form)  
    {
        Form->setObjectName(QStringLiteral("Form"));
        Form->resize(600, 100);
        pushButton = new QPushButton(Form);
        pushButton->setObjectName(QStringLiteral("pushButton"));
        pushButton->setGeometry(QRect(10, 50, 580, 33));
        pushButton->connect(pushButton,&QAbstractButton::clicked,outer_func);
        pushButton->connect(pushButton,&QAbstractButton::clicked,a_functor);
        pushButton->connect(pushButton,&QAbstractButton::clicked,[=] (bool){std::printf("This is a test by lambda;)");});


Form->setObjectName(QStringLiteral("Form"));
        Form->setWindowTitle(QString("Form"));
Form->resize(600, 100);
        pushButton->setText(QString("connect functor and outer class func simultaneously"));
pushButton = new QPushButton(Form);
    } // setupUi
pushButton->setObjectName(QStringLiteral("pushButton"));
};
pushButton->setGeometry(QRect(10, 50, 580, 33));
pushButton->connect(pushButton,&QAbstractButton::clicked,outer_func);
pushButton->connect(pushButton,&QAbstractButton::clicked,a_functor);
pushButton->connect(pushButton,&QAbstractButton::clicked,[=] (bool){std::printf("This is a test by lambda;)");});


Form->setWindowTitle(QString("Form"));
#endif // TEST_H
pushButton->setText(QString("connect functor and outer class func simultaneously"));
} // setupUi
};
#endif // TEST_H
</code>
</code>
and test.pro
and test.pro
<code>
<code>
TEMPLATE = app
TEMPLATE = app
TARGET = test
TARGET = test
INCLUDEPATH''= .
INCLUDEPATH += .
QT ''= widgets # mandatory needed for gui on qt5
QT += widgets # mandatory needed for gui on qt5
QMAKE_CXXFLAGS''= -std=c+''11 # needed for lambda
CONFIG += c++11 # needed for lambda
HEADERS''= test.h
HEADERS += test.h
SOURCES += main.cpp
SOURCES += main.cpp
</code>

Latest revision as of 21:04, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

This small (cross platform) application demonstrates a few new features that come with Qt 5: the ability to connect to C++ functor objects, non member outer class functions and c++11 lambdas.

main.cpp:

#include "test.h"

void outer_func() { std::printf("this is a test by outer class func;)"); }
void functor::operator() (bool) { std::printf("this is a test by functor;)"); }

int main(int argc,char* argv[]) {
    QApplication app(argc, argv);
    Ui_Form ui;
    QWidget* form=new QWidget();
    ui.setupUi(form);
    form->show();
    return app.exec();
}

test.h

#ifndef TEST_H
#define TEST_H

#include <QVariant>
#include <QAction>
#include <QApplication>
#include <QButtonGroup>
#include <QHeaderView>
#include <QPushButton>
#include <QWidget>
#include <cstdio>

void outer_func();

class functor
{
public:
    void operator() (bool);
};

functor a_functor; // create an instance of functor

class Ui_Form
{
public:
    QPushButton *pushButton;
    void setupUi(QWidget *Form) 
    {
        Form->setObjectName(QStringLiteral("Form"));
        Form->resize(600, 100);
        pushButton = new QPushButton(Form);
        pushButton->setObjectName(QStringLiteral("pushButton"));
        pushButton->setGeometry(QRect(10, 50, 580, 33));
        pushButton->connect(pushButton,&QAbstractButton::clicked,outer_func);
        pushButton->connect(pushButton,&QAbstractButton::clicked,a_functor);
        pushButton->connect(pushButton,&QAbstractButton::clicked,[=] (bool){std::printf("This is a test by lambda;)");});

        Form->setWindowTitle(QString("Form"));
        pushButton->setText(QString("connect functor and outer class func simultaneously"));
    } // setupUi
};

#endif // TEST_H

and test.pro

TEMPLATE = app
TARGET = test
INCLUDEPATH += .
QT += widgets # mandatory needed for gui on qt5
CONFIG += c++11 # needed for lambda
HEADERS += test.h
SOURCES += main.cpp