Δρ Γ Χαμόδρακας‘νάπτυξη... · qt/ gnuplot •qt: a cross‐platform...
TRANSCRIPT
Qt 5ApplicationFrameworkGnuplot PlottingLibrary
Δρ.Γ.Χαμόδρακας
Qt /Gnuplot
• Qt:across‐platformapplicationframework– Mainuse:C++applicationdevelopmentwithGraphicalUserInterfaces
– Currentversion:Qt 5• Gnuplot:aprogramgenerating2and3‐dimensionalplotsoffunctionsordata– CanbebuiltasalibraryprovidingQtGnuplotWidgetclass
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 2
Qt 5installation
Ubuntudistribution:
$sudo apt-get install build-essential mesa-common-dev$sudo apt-get install libglu1-mesa-dev -y $sudo apt-get install libxt-dev qt5-default qtquick1-5-dev qtdeclarative5-dev qtscript5-dev libqt5webkit5-dev qttools5-dev-tools qtcreator$sudo apt-get install ^libqt5svg5
If Ubunturuns asaVirtualBox Appliance 3dacceleration must bedisabled.
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 3
Gnuplot Installation(1)
BuildGnuplot asalibrary$sudo apt-get install automake libtool cvs
$cvs -d:pserver:[email protected]:/cvsroot/gnuplot login$cvs -z3 -d:pserver:[email protected]:/cvsroot/gnuplot co -P gnuplot
$cd gnuplot$wget http://sourceforge.net/p/gnuplot/patches/_discuss/thread/47917802/fbe5/attachment/qtlib6.patch
$patch -p0 < qtlib6.patch$libtoolize
$./prepare$./configure --with-qt=lib$export QT_PATH=/usr
$export UIC=$QT_PATH/bin/uic$export MOC=$QT_PATH/bin/moc$export RCC=$QT_PATH/bin/rcc$export CXX=g++
$export CC=gcc
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 4
Gnuplot Installation(2)
$export QT_LIBS="-Wl,--no-as-needed /usr/lib/x86_64-linux-gnu/libQt5Concurrent.so \
/usr/lib/x86_64-linux-gnu/libQt5Core.so /usr/lib/x86_64-linux-gnu/libQt5DBus.so \/usr/lib/x86_64-linux-gnu/libQt5Declarative.so /usr/lib/x86_64-linux-gnu/libQt5Gui.so \/usr/lib/x86_64-linux-gnu/libQt5Network.so /usr/lib/x86_64-linux-gnu/libQt5OpenGL.so \/usr/lib/x86_64-linux-gnu/libQt5PrintSupport.so \
/usr/lib/x86_64-linux-gnu/libQt5Qml.so \/usr/lib/x86_64-linux-gnu/libQt5QuickParticles.so \/usr/lib/x86_64-linux-gnu/libQt5Quick.so /usr/lib/x86_64-linux-gnu/libQt5QuickTest.so \
/usr/lib/x86_64-linux-gnu/libQt5QuickWidgets.so \/usr/lib/x86_64-linux-gnu/libQt5Script.so \/usr/lib/x86_64-linux-gnu/libQt5ScriptTools.so /usr/lib/x86_64-linux-gnu/libQt5Sql.so \/usr/lib/x86_64-linux-gnu/libQt5Svg.so /usr/lib/x86_64-linux-gnu/libQt5Test.so \
/usr/lib/x86_64-linux-gnu/libQt5WebKit.so \/usr/lib/x86_64-linux-gnu/libQt5WebKitWidgets.so \/usr/lib/x86_64-linux-gnu/libQt5Widgets.so /usr/lib/x86_64-linux-gnu/libQt5Xml.so"
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 5
Gnuplot Installation(3)$export QT_CFLAGS="-F$QT_PATH -I/usr/include/x86_64-linux-gnu/qt5 \-I/usr/include/x86_64-linux-gnu/qt5/QtConcurrent \-I/usr/include/x86_64-linux-gnu/qt5/QtCore \ -I/usr/include/x86_64-linux-gnu/qt5/QtDBus \-I/usr/include/x86_64-linux-gnu/qt5/QtDeclarative \-I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtNetwork \-I/usr/include/x86_64-linux-gnu/qt5/QtOpenGL \-I/usr/include/x86_64-linux-gnu/qt5/QtOpenGLExtensions \-I/usr/include/x86_64-linux-gnu/qt5/QtPlatformHeaders \-I/usr/include/x86_64-linux-gnu/qt5/QtPrintSupport \-I/usr/include/x86_64-linux-gnu/qt5/QtQml -I/usr/include/x86_64-linux-gnu/qt5/QtQuick \-I/usr/include/x86_64-linux-gnu/qt5/QtQuickParticles \-I/usr/include/x86_64-linux-gnu/qt5/QtQuickTest \-I/usr/include/x86_64-linux-gnu/qt5/QtQuickWidgets \-I/usr/include/x86_64-linux-gnu/qt5/QtScript \-I/usr/include/x86_64-linux-gnu/qt5/QtScriptTools \-I/usr/include/x86_64-linux-gnu/qt5/QtSql -I/usr/include/x86_64-linux-gnu/qt5/QtTest \-I/usr/include/x86_64-linux-gnu/qt5/QtWebKit \-I/usr/include/x86_64-linux-gnu/qt5/QtWebKitWidgets \-I/usr/include/x86_64-linux-gnu/qt5/QtWidgets -I/usr/include/x86_64-linux-gnu/qt5/QtSvg \-I/usr/include/x86_64-linux-gnu/qt5/QtXml"
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 6
Gnuplot Installation(4)
• Hack the qtterminal ofGnuplot:
Create apublic function QLabel * getStatusLabel() const returning m_statusLabel private memberinQtGnuplotWidget class (src/qtterminal/QtGnuplotWidget.h & src/qtterminal/QtGnuplotWidget.cpp)
add #include <cmath> in src/qtterminal/qt_conversion.cpp
delete demosubdir from SUBDIRSvariableinMakefile with editor(inGnuplot root path)
Runthe following commands within Gnuplot root path:$make
$sudo make install
download QtExample.zipfrom e‐class andunzip inaproper dir
$qtcreator
openQtExample project
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 7
Qt 5Tutorial
• Qt modules:– Qt Library:C++classlibraryprovidingapplicationbuildingblocksandGUIwidgetclasses
– Qt Creator:AnIDEforQt Applications– Qt Designer:AtoolofQt CreatorfordesigningGUIs,generating.ui xmlfiles
– qmake:toolforgeneratingMakefile– Thereisaplug‐inforEclipseIDE
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 8
Qt Creator:Helloworldproject
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 9
HelloWorldproject(cont.d)
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 10
HelloWorldproject(cont.d)
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 11
HelloWorld:mainfunction
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 12
HelloWorld:mainfunction(cont.d)
• TheQApplication objectcontainsthemaineventloop,wherealleventsfromthewindowsystem,thekeyboard,themouse,etc.areprocessedanddispatched.
• ThereisonlyoneQApplication objectinaQt GUIapplication.• app.exec();makestheQApplication enteriseventloop.• Theapplicationiscompiledandexecutedwiththefirstgreen
“Play”button.• The“Play”buttonwiththebuginitiatesadebuggingsession.• The“Hammer”buttonbuildstheproject• Inordertobuildtheprojectfromcommandlinerun:
$qmake (generatestheMakefile)$make
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 13
HelloWorld:mainfunction(cont.d)
• AQt GUIcomponentisdefinedinthemainclassaftertheQApplication
• Usuallythemainwindowoftheappisdefinedhere• HelloWorldclassinheritsQMainWindow class• Themainwindowisshownwiththecall:w.show();• QMainWindow inheritsQWidget class
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 14
Qt ClassHierarchy(fragment)
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 15
Projectconfigurationfile
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 16
Projectconfigurationfile(cont.d)
• QT:indicateswhatlibrariesareusedintheproject• TARGET:indicatesthenameoftheapplication• TEMPLATE:indicatesthebuildtype(apporlibrary)• SOURCES/HEADERS:sourceandheaderfilesoftheapp(apartfromgeneratedones)
• FORMS:xmlfilesrepresentingtheGUIstructureofawidget
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 17
Qt Designer
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 18
Qt Designer
• GUIcomponents(widgets)canbeaddedtoawindowwithQt Designer.
• Widgetsareaddedwithdraganddrop.• WidgetdesignpropertiescanbesetbythepropertyeditorshowninthelowerleftsideofQt Designerview.
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 19
Generatedhelloworld.ui file
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 20
HelloWorldclassdefinition
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 21
HelloWorldclassdefinition(cont.d)
• Qt extendsC++withameta‐objectsystemallowingrun‐timeintrospectionwhichenablesthesignal‐slotobjectcommunicationmechanism,dynamicproperties,etc.
• GUIcomponentsmustcontaintheQ_OBJECTmacrotosupporttheextensions.
• Themoc meta‐objectcompilerreadstheC++headerfile.IfitfindsoneormoreclassdeclarationsthatcontaintheQ_OBJECTmacro,itproducesaC++sourcefilecontainingthemeta‐objectcodeforthoseclasses.
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 22
HelloWorldclassdefinition(cont.d)
• Theexplicitkeywordintheconstructorpreventsimplicitconversionstoinstantiateanobject
• TheconstructorargumentdefinestheparentofaQObject.Thus,objecthierarchiesarecreated.
• ObjecthierarchiesarepartofQt memorymanagement.WhenaparentQObjectisdeleted,allitschildreninthehierarchyarealsodeleted.
• ChildGUIcomponentsarerenderedinthesamewindow.• ThecodefortheUi::HelloWorldclassisgeneratedbyuic(UserInterfaceCompiler)accordingtotheui xmlfile.
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 23
GeneratedUIheaderfile
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 24
HelloWorldclassimplementation
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 25
HelloWorldimplementation(cont.d)
• TheconstructorofHelloWorldinvokestheconstructorsofQMainWindow andUi::HelloWorldgeneratedclass.
• TheUserInterfacecomponentsareinitializedwithsetupUi(this)invocation.
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 26
SignalsandSlots
• Thecommunicationbetweenobjectsisperformedthroughthesignal‐slotmechanism.
• Signalsareemittedfromobjectswhenspecificeventshappen.
• Qt widgetshavepredefinedsignals.Widgetsaresubclassedtoadduser‐definedsignals.
• SignalsarepublicaccessfunctionsinaQObjectwithnoreturntypes(returnvoid).
• Itisrecommendedtoemitthemonlyfromtheclassitselfanditssubclasses.
• Signalsareautomaticallygeneratedbythemeta‐objectcompilerandmustnotbeimplementedinthe.cpp file
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 27
SignalsandSlots
• SlotsarenormalmemberC++functionsdeclaredasslots.• Aslotiscalledwhenasignalconnectedtoitisemitted.• Slotsarecalledwhenaconnectedsignalisemittedregardlessoftheiraccesslevel(evenifprivate).
• Thesignatureofasignalmustmatchthesignatureofthereceivingslot.
• Signalsareconnectedwithslotsthroughthecallof:QObject::connect(const QObject * sender,const char* signal,const QObject *receiver,const char*method,Qt::ConnectionType type =Qt::AutoConnection)
• Thereareslotsautomaticallyconnectedwithsignalsthroughtheuserinterfacecompiler(accordingtostandardnamingconventions)
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 28
Signal/Slotexample
#include <QObject>
class Counter : public QObject { Q_OBJECT
public: Counter() {
m_value = 0; } int value() const {
return m_value; } public slots:
void setValue(int value);
signals: void valueChanged(int newValue);
private: int m_value;
};
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 29
void Counter::setValue(int value)
{if (value != m_value) {
m_value = value;emit valueChanged(value);
}}...
Counter a, b;QObject::connect(&a,&Counter::valueChanged,
&b, &Counter::setValue);/*a.value() == 12, b.value() == 12*/
a.setValue(12);/* a.value() == 12, b.value() == 48*/b.setValue(48);
...
Signal/Slotautomaticconnection
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 30
Signal/Slotautomaticconnection(2)
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 31
Eventsystem
• InQt,eventsareobjects,derivedfromtheabstractQEventclass,thatrepresentthingsthathavehappenedeitherwithinanapplicationorasaresultofoutsideactivitythattheapplicationneedstoknowabout.
• Standardeventclasses:QResizeEvent, QPaintEvent, QMouseEvent, QKeyEvent, and QCloseEvent
• Thenormalwayforaneventtobedeliveredisbycallingavirtualfunction.Ifyoudonotperformallthenecessaryworkinyourimplementationofthevirtualfunction,youmayneedtocallthebaseclass'simplementation.
• EventsaredeliveredtoaspecificQObjectaccordingtotheircontext.
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 32
Eventhandlerexample
void MyCheckBox::mousePressEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton) {// handle left mouse button here
} else {// pass on other buttons to base classQCheckBox::mousePressEvent(event);
}}
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 33
Eventfilters
• Sometimesanobjectneedstolookat,andpossiblyintercept,theeventsthataredeliveredtoanotherobject.
Visualization::Visualization(QWidget *parent) :QMainWindow(parent),ui(new Ui::Visualization)
{ui->setupUi(this);widget = new QtGnuplotWidget();widget->installEventFilter(this);widget->setStatusLabelActive(true);instance = new QtGnuplotInstance();instance->setWidget(widget);
}
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 34
Eventfilters(2)
bool Visualization::eventFilter(QObject *obj, QEvent *event)
{if (event->type() == QEvent::MouseButtonPress){
if (obj == this->widget) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);if (mouseEvent->button() == Qt::LeftButton) {
ui->outputTxt->setText( this->widget->getStatusLabel()->text());
}}
}return QObject::eventFilter(obj, event);
}
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 35
QtGnuplot
• QtGnuplot libraryprovides2basicclasses:– QtGnuplotWidget (subclassofQWidget)– QtGnuplotInstance
• ThefirstclassisaQWidget whereGnuplot plotsarepainted.
• ThesecondclassisaninterfacetotheGnuplot program
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 36
QtGnuplot example
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 37
QtGnuplot example(2)
• Theuncommentedlineplotsacircle.Inordertodothatwetakethecontourlineofthefunctionz=f(x,y)atz=0
• Thecommentedlineplotsapointina2‐dimensionaldiagram.• DocumentationconcerningGnuplot commandscanbefoundin:
http://www.gnuplot.info/docs_4.0/gpcard.pdf• Helpfulmaterialcanbefoundin:
http://www.gnuplotting.org/plotting‐single‐points/ andhttp://www.gnuplotting.org/plotting‐data/
Κ23γ‐ ΑνάπτυξηΛογισμικούγιαΑλγοριθμικάΠροβλήματα:Φροντιστήριο 38