1 / 19

First Steps with Qt

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 (); }.

ozzy
Download Presentation

First Steps with Qt

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. First Steps with Qt JulienFinet Kitware Inc. Jan. 05th 2010

  2. Hello World! #include <QApplication> #include <QPushButton> int main(intargc, 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.

  3. Qt classes QObject QPaintDevice QAction … QWidget QAbstractButton … QLabel QCheckBox QPushButton

  4. 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

  5. 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).

  6. QObjects • QObjects can be safely created on the heap. • Be careful when instantiating on the stack. … int main(intargc, char* argv[]) { … QWidgetparentWidget; QPushButton* button = new QPushButton(“Hello World”, parentWidget); … } parentWidget’s destructor deletes button int main(intargc, char* argv[]) { … QWidgetparentWidget; QPushButton button(“Hello World”, &parentWidget); … } int main(intargc, char* argv[]) { … QPushButtonbutton(“Hello World”); QWidgetparentWidget; button.setParent(&parentWidget); … } The destructor of button is called first, and it removes itself from its parent. Error: button is deleted twice

  7. 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; … … QStringfirstName(“John”); QStringlastName(“Doe”); Qvector<int> ages; … ages.resize(10); …

  8. 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

  9. QLayout class diagram QObject QLayout QBoxLayout QFormLayout QGridLayout QStackedLayout QHBoxLayout QVBoxLayout

  10. 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); …

  11. 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); …

  12. 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

  13. Signals & Slots

  14. Signals & Slots - Example … 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))); … } valueChanged() is not defined ... class QSlider: public QWidget { Q_OBJECT public: … public slots: void setValue(int value); … signals: void valueChanged(int value); … }; … void QSlider::setValue(intvalue) { … if (value != old) { emit valueChanged(value); }} … ... class QSpinBox: public QWidget { Q_OBJECT public: … public slots: void setValue(int value); … signals: void valueChanged(int value); … }; … void QSlider::setValue(intvalue) { … if (value != old) { emit valueChanged(value); }} … QSpinBox.cpp QSpinBox.h

  15. Signals & Slots - Example PROJECT(MyProject) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) SET(MyProject_SRCS MyWidget.cxx main.cxx) SET(MyProject_MOC_SRCSMyWidget.h) QT4_WRAP_CPP(MyProject_SRCS ${MyProject_MOC_SRCS}) ADD_EXECUTABLE( MyProject ${MyProject_SRCS}) TARGET_LINK_LIBRARIES(MyProject ${QT_LIBRARIES}) #include <QApplication> #include “MyWidget.h” intmaint(intargc, char* argv[]) { QApplication app(argc, argv); QGroupBoxcontainer; MyWidget w1(&container); MyWidget w2(&container); QObject::connect(w1, SIGNAL(valueChanged(int)), w2, SLOT(setValue(int))); container.show(); return app.exec();} main.cxx CMakeLists.cxx #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); }} #include <QWidget> class MyWidget: public QWidget { Q_OBJECT public: MyWidget(QWidget* parent = 0); public slots: void setValue(int value); signals: void valueChanged(int value); private: int Value;}; MyWidget.h MyWidget.cxx

  16. 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(button, SIGNAL(clicked()), widget2, SLOT(updateScreen())); … QObject::connect(button, SIGNAL(clicked()), widget2, SLOT(updateScreen())); … … QObject::connect(loginPushbutton, SIGNAL(clicked()), this, SIGNAL(loginRequested())); … class MyWidget: public QObject { … signals: void mySignal(int value = 5); … }; … emit mySignal(); … emit mySignal(10); … MyWidget.cxx MyWidget.h

  17. 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())); … MyWidget.cxx MyWidget.h … QObject::connect(slider, SIGNAL(valueChanged(int)), this, SLOT(update())); …

  18. Qt Designer

  19. 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 { …}; CMakeLists.cxx MyWidget.h #include <QApplication> #include “MyWidget.h” intmaint(intargc, char* argv[]) { QApplication app(argc, argv); MyWidget w(0); w.show(); return app.exec();} #include “MyWidget.h” #include “"ui_MyWidget.h” void MyWidget::MyWidget(QWidget* parent) : QWidget(parent) { this->setupUi(this); } MyWidget.cxx main.cxx

More Related