|
| 1 | +#include "HelloQt.hpp" |
| 2 | +#include <QHBoxLayout> |
| 3 | +#include <QVBoxLayout> |
| 4 | +#include <QPushButton> |
| 5 | +#include <QLabel> |
| 6 | +#include <QDebug> |
| 7 | + |
| 8 | +HelloQt::HelloQt(QWidget *parent) : QWidget(parent) { |
| 9 | + // The memory management is not done explicitly but by using the parent |
| 10 | + // relationship built into Qt |
| 11 | + // The tr calls are not strictly neccesary, but it is all that is needed for |
| 12 | + // future i18n. |
| 13 | + auto mainLayout = new QVBoxLayout; |
| 14 | + auto descriptiveLabel = new QLabel(tr("Descriptive")); |
| 15 | + auto pushButton = new QPushButton(tr("Push me!")); |
| 16 | + auto grouping = new QHBoxLayout; |
| 17 | + auto directButton = new QPushButton(tr("Directly!")); |
| 18 | + auto fatal = new QPushButton(tr("Don't push this button")); |
| 19 | + // By adding the widgets to the main widget, they get parented and the |
| 20 | + // parenting takes care of freeing the memory. |
| 21 | + // Laying out in layouts makes the GUI automatically resize. |
| 22 | + grouping->addWidget(descriptiveLabel); |
| 23 | + grouping->addWidget(pushButton); |
| 24 | + mainLayout->addLayout(grouping); |
| 25 | + mainLayout->addWidget(directButton); |
| 26 | + mainLayout->addWidget(fatal); |
| 27 | + setLayout(mainLayout); |
| 28 | + // Signals and slots are use to simply define relationships between actions |
| 29 | + // and effects |
| 30 | + // By using this "new style" connect the types are checked at compile time to |
| 31 | + // match |
| 32 | + connect(pushButton, &QPushButton::clicked, this, &HelloQt::writeToDebug); |
| 33 | + // Lambdas can also be used as can ordinary freestanding functions |
| 34 | + connect(directButton, &QPushButton::clicked, |
| 35 | + []() { qDebug() << tr("I am in line"); }); |
| 36 | + // Most usefull signals and slots are already built into the framework. |
| 37 | + connect(fatal, &QPushButton::clicked, this, &HelloQt::close); // could just use close |
| 38 | +}; |
| 39 | + |
| 40 | +HelloQt::~HelloQt() { |
| 41 | + // Nothing to do here, the parent system makes sure that all the elements that |
| 42 | + // are parented to this is cleaned up |
| 43 | +} |
| 44 | + |
| 45 | +void HelloQt::writeToDebug(void) { |
| 46 | + qDebug() << tr("I don't know what I was supposed to debug here..."); |
| 47 | +} |
0 commit comments