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

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
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:<br /><code><br /> #include "test.h"
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:
<br /> void outer_func(){std::printf("this is a test by outer class func;)");}<br /> void functor::operator() (bool) {std::printf("this is a test by functor;)");}
<code>
<br /> int main(int argc,char* argv[]){<br /> QApplication app(argc, argv);<br /> Ui_Form ui;<br /> QWidget* form=new QWidget();<br /> ui.setupUi(form);<br /> form->show();<br /> return app.exec();<br /> }<br /></code><br />test.h<br /><code><br /> #ifndef TEST_H<br /> #define TEST_H
#include "test.h"
<br /> #include <QtCore/QVariant><br /> #include <QtWidgets/QAction><br /> #include <QtWidgets/QApplication><br /> #include <QtWidgets/QButtonGroup><br /> #include <QtWidgets/QHeaderView><br /> #include <QtWidgets/QPushButton><br /> #include <QtWidgets/QWidget><br /> #include <cstdio>
 
<br /> void outer_func();<br /> class functor{public:void operator() (bool);};
void outer_func(){std::printf("this is a test by outer class func;)");}
<br /> functor a_functor; // create an instance of functor
void functor::operator() (bool) {std::printf("this is a test by functor;)");}
<br /> class Ui_Form{
 
<br /> public:<br /> QPushButton *pushButton;<br /> void setupUi(QWidget *Form){
int main(int argc,char* argv[]){
<br /> Form->setObjectName(QStringLiteral("Form"));<br /> Form->resize(600, 100);<br /> pushButton = new QPushButton(Form);<br /> pushButton->setObjectName(QStringLiteral("pushButton"));<br /> pushButton->setGeometry(QRect(10, 50, 580, 33));<br /> pushButton->connect(pushButton,&amp;QAbstractButton::clicked,outer_func);<br /> pushButton->connect(pushButton,&amp;QAbstractButton::clicked,a_functor);<br /> pushButton->connect(pushButton,&amp;QAbstractButton::clicked,[=] (bool){std::printf("This is a test by lambda;)");});
QApplication app(argc, argv);
<br /> Form->setWindowTitle(QString("Form"));<br /> pushButton->setText(QString("connect functor and outer class func simultaneously"));<br /> } // setupUi<br /> };<br /> #endif // TEST_H<br /></code><br />and test.pro<br /><code><br /> TEMPLATE = app<br /> TARGET = test<br /> INCLUDEPATH''= .<br /> QT ''= widgets # mandatory needed for gui on qt5<br /> QMAKE_CXXFLAGS''= -std=c+''11 # needed for lambda<br /> HEADERS''= test.h<br /> SOURCES += main.cpp
Ui_Form ui;
QWidget* form=new QWidget();
ui.setupUi(form);
form->show();
return app.exec();
}
</code>
test.h
<code>
#ifndef TEST_H
#define TEST_H
 
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/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,&amp;QAbstractButton::clicked,outer_func);
pushButton->connect(pushButton,&amp;QAbstractButton::clicked,a_functor);
pushButton->connect(pushButton,&amp;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
</code>
and test.pro
<code>
TEMPLATE = app
TARGET = test
INCLUDEPATH''= .
QT ''= widgets # mandatory needed for gui on qt5
QMAKE_CXXFLAGS''= -std=c+''11 # needed for lambda
HEADERS''= test.h
SOURCES += main.cpp

Revision as of 11:21, 25 February 2015

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:

 #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 <QtCore/QVariant>
 #include <QtWidgets/QAction>
 #include <QtWidgets/QApplication>
 #include <QtWidgets/QButtonGroup>
 #include <QtWidgets/QHeaderView>
 #include <QtWidgets/QPushButton>
 #include <QtWidgets/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,&amp;QAbstractButton::clicked,outer_func);
 pushButton->connect(pushButton,&amp;QAbstractButton::clicked,a_functor);
 pushButton->connect(pushButton,&amp;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
QMAKE_CXXFLAGS= -std=c+11 # needed for lambda
HEADERS= test.h
SOURCES += main.cpp