first steps with qt julien finet kitware inc. jan. 05 th 2010

19
First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Upload: melanie-payne

Post on 27-Mar-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

First Steps with Qt

Julien FinetKitware Inc.Jan. 05th 2010

Page 2: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Hello World!

#include <QApplication>#include <QPushButton>

int main(int argc, char * argv[]){ QApplication app(argc, argv);

QPushButton button(“Hello World”);

button.show();

return app.exec();}

The QApplication object must be created before any GUI-related features of Qt are used.

Enter the loop of events. Qt receives and processes user and system events and passes these on to the appropriate widgets.

Page 3: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Qt classesQObject

QWidget

QLabelQAbstractButton

QPushButtonQCheckBox

QAction

QPaintDevice

Page 4: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

QObjects

• The QObject class is the base class of all Qt objects

• When deleted, the parent deletes its children.

class QObject{public: QObject(QObject * parent = 0);

virtual ~QObject();

const QObjectList & children () const;

void setParent(QObject * parent);};

Add itself to the parent’s children list.

qobject.h

Page 5: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

QWidgets

• If a widget’s parent is 0, the widget is a window.

• When shown/hidden, the parent shows/hides its children.

• When enabled/disabled, the parent enables/disables its children (except for children explicitly hidden).

Page 6: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

QObjects

• QObjects can be safely created on the heap.

• Be careful when instantiating on the stack.

…int main(int argc, char* argv[]){ … QWidget parentWidget; QPushButton* button = new QPushButton(“Hello World”, parentWidget); …}

parentWidget’s destructor deletes button

int main(int argc, char* argv[]){ … QWidget parentWidget; QPushButton button(“Hello World”, & parentWidget); …}

int main(int argc, char* argv[]){ … QPushButton button(“Hello World”); QWidget parentWidget; button.setParent(&parentWidget); …}

Error: button is deleted twiceThe destructor of button is called first, and it removes itself from its parent.

Page 7: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Non QObject

• May be instantiated on the heap or stack.

• It’s safer to create them on the stack.

…QVector<int>* ages;…ages = new QVector<int>(10);…delete ages;…

…QString firstName(“John”);QString lastName(“Doe”);Qvector<int> ages;…ages.resize(10);…

Page 8: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

QLayout – Geometry Manager

• Instead of specifying widget coordinates for each QWidget, use QLayouts.

…QGroupBox* parameters = new QGroupBox(“Parameters”, this);QLabel* label = new QLabel(“Number of Iterations:”, parameters);QSpinBox* spinBox = new QSpinBox(parameters);…label->setGeometry(0, 0, 50, 20);spinBox->setGeometry(56, 0, 30, 20);…

…QGroupBox* parameters = new QGroupBox(“Parameters, this);QLabel* label = new QLabel(“Number of Iterations:”, parameters);QSpinBox* spinBox = new QSpinBox(parameters);…QHBoxLayout* layout = new QHBoxLayout;layout->addWidget(label);layout->addWidget(spinBox);parameters->setLayout(layout);…Without QLayout With QLayout

Page 9: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

QLayout class diagram

QObject

QLayout

QStackedLayoutQBoxLayout

QVBoxLayoutQHBoxLayout

QGridLayoutQFormLayout

Page 10: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

QLayout Example 1/2

• QHBoxLayout

• QVBoxLayout

…QHBoxLayout* layout = new QHBoxLayout;layout>addWidget(iterationNumberLabel);layout->addWidget(iterationNumberSpinBox);layout->addWidget(thresholdLabel);layout->addWidget(thresholdDoubleSpinBox);parameters->setLayout(layout);…

…QVBoxLayout* layout = new QVBoxLayout;layout>addWidget(iterationNumberLabel);layout->addWidget(iterationNumberSpinBox);layout->addWidget(thresholdLabel);layout->addWidget(thresholdDoubleSpinBox);parameters->setLayout(layout);…

Page 11: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Qlayout Example 2/2

• QGridLayout

• QFormLayout

QGridLayout* layout = new QGridLayout;layout>addWidget(iterationNumberLabel, 0, 0, 1, 1);layout->addWidget(iterationNumberSpinBox , 0, 1, 1, 1);layout->addWidget(thresholdLabel, 1, 0, 1, 1);layout->addWidget(thresholdDoubleSpinBox, 1, 1, 1, 1);parameters->setLayout(layout);…

QFormLayout* layout = new QFormLayout;layout>setWidget(0, QFormLayout::LabelRole, iterationNumberLabel);layout->setWidget(0, QFormLayout::FieldRole, iterationNumberSpinBox);layout->setWidget(1, QFormLayout::LabelRole, thresholdLabel);layout->addWidget(1, QFormLayout::FieldRole, thresholdDoubleSpinBox);parameters->setLayout(layout);…

Page 12: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Signals & Slots

• Qt uses signals and slots for high-level events (listerners) and virtual methods for low-level events

• Type-safe callbacks

• Precompiler (moc)

• A class which emits a signal neither knows nor cares which slots receive the signal

Page 13: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Signals & Slots

Page 14: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Signals & Slots - Example

...class QSlider: public QWidget{ Q_OBJECTpublic: …public slots: void setValue(int value); …signals: void valueChanged(int value); …};

…void QSlider::setValue(int value){ … if (value != old) { emit valueChanged(value); }}…

valueChanged() is not defined

…int main(…){ … QSpinBox* spinBox = new QSpinBox(panel); QSlider* slider = new QSlider(panel); … QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue (int))); QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue (int))); …}

...class QSpinBox: public QWidget{ Q_OBJECTpublic: …public slots: void setValue(int value); …signals: void valueChanged(int value); …};

…void QSlider::setValue(int value){ … if (value != old) { emit valueChanged(value); }}…

QSpinBox.h QSpinBox.cpp

Page 15: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Signals & Slots - ExamplePROJECT(MyProject)

FIND_PACKAGE(Qt4 REQUIRED)INCLUDE(${QT_USE_FILE})

SET(MyProject_SRCS MyWidget.cxx main.cxx)SET(MyProject_MOC_SRCS MyWidget.h)

QT4_WRAP_CPP(MyProject_SRCS ${MyProject_MOC_SRCS})

ADD_EXECUTABLE( MyProject ${MyProject_SRCS})

TARGET_LINK_LIBRARIES(MyProject ${QT_LIBRARIES})

#include <QWidget>class MyWidget: public QWidget{ Q_OBJECTpublic: MyWidget(QWidget* parent = 0);public slots: void setValue(int value);signals: void valueChanged(int value);private: int Value;};

#include “MyWidget.h”

void MyWidget::MyWidget(QWidget* parent) : QWidget(parent){}

void MyWidget::setValue(int value){ if (value != this->Value) { value = this->Value; emit this->valueChanged(this->Value); }}MyWidget.h

MyWidget.cxx

CMakeLists.cxx

#include <QApplication>#include “MyWidget.h”

int maint(int argc, char* argv[]){ QApplication app(argc, argv); QGroupBox container; MyWidget w1(&container); MyWidget w2(&container);

QObject::connect(w1, SIGNAL(valueChanged(int)), w2, SLOT(setValue(int))); container.show(); return app.exec();} main.cxx

Page 16: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Signals & Slots – Good to remember 1

• Every connection you make emits a signal– duplicate connections emit two signals.

– break a connection using disconnect()• You can connect a signal to a signal

• Signals can have default values

• _

…QObject::connect(loginPushbutton, SIGNAL(clicked()), this, SIGNAL(loginRequested()));…

class MyWidget: public QObject{ …signals: void mySignal(int value = 5); …};

… emit mySignal(); … emit mySignal(10); …

…QObject::connect(button, SIGNAL(clicked()), widget2, SLOT(updateScreen()));…QObject::connect(button, SIGNAL(clicked()), widget2, SLOT(updateScreen()));…

MyWidget.hMyWidget.cxx

Page 17: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

Signals & Slots – Good to remember 2

• Slots are implemented as normal methods– Slots can be virtual, public, protected, private.

• Slot may have a shorter signature than the signal it receives– slots can ignore extra arguments.

class MyWidget: public QObject{ …protected slots: virtual void myAbstractSlot() = 0; …};

…QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(myAbstractSlot()));…

…QObject::connect(slider, SIGNAL(valueChanged(int)), this, SLOT(update()));…

MyWidget.hMyWidget.cxx

Page 19: First Steps with Qt Julien Finet Kitware Inc. Jan. 05 th 2010

QtDesigner- Example

…SET(MyProject_UI_SRCS Resources/UI/MyProject.ui)…QT4_WRAP_UI(MyProject_UI_CXX ${MyProject_UI_SRCS})…

#include <QWidget>#include “ui_MyWidget.h”

class MyWidget: public Qwidget, Ui_MyWidget{ …};

#include “MyWidget.h”#include “"ui_MyWidget.h”

void MyWidget::MyWidget(QWidget* parent) : QWidget(parent){ this->setupUi(this);}

MyWidget.h

MyWidget.cxx

CMakeLists.cxx

#include <QApplication>#include “MyWidget.h”

int maint(int argc, char* argv[]){ QApplication app(argc, argv); MyWidget w(0); w.show(); return app.exec();} main.cxx