Sending a sequence of commands and wait for response

Let’s use QStateMachine to make this simple. Let’s recall how you wished such code would look: Serial->write(“boot”, 1000); Serial->waitForKeyword(“boot successful”); Serial->sendFile(“image.dat”); Let’s put it in a class that has explicit state members for each state the programmer could be in. We’ll also have action generators send, expect, etc. that attach given actions to states. // … Read more

In a templated derived class, why do I need to qualify base class member names with “this->” inside a member function?

C++ answer (general answer) Consider a template class Derived with a template base class: template <typename T> class Base { public: int d; }; template <typename T> class Derived : public Base<T> { void f () { this->d = 0; } }; this has type Derived<T>, a type which depends on T. So this has … Read more

Connecting overloaded signals and slots in Qt 5

The problem here is that there are two signals with that name: QSpinBox::valueChanged(int) and QSpinBox::valueChanged(QString). From Qt 5.7, there are helper functions provided to select the desired overload, so you can write connect(spinbox, qOverload<int>(&QSpinBox::valueChanged), slider, &QSlider::setValue); For Qt 5.6 and earlier, you need to tell Qt which one you want to pick, by casting it … Read more

Passing an argument to a slot

With Qt 5 and a C++11 compiler, the idiomatic way to do such things is to give a functor to connect: connect(action1, &QAction::triggered, this, [this]{ onStepIncreased(1); }); connect(action5, &QAction::triggered, this, [this]{ onStepIncreased(5); }); connect(action10, &QAction::triggered, this, [this]{ onStepIncreased(10); }); connect(action25, &QAction::triggered, this, [this]{ onStepIncreased(25); }); connect(action50, &QAction::triggered, this, [this]{ onStepIncreased(50); }); The third argument to … Read more

QLabel does not display in QWidget

Widgets when they are visible for the first time call to be visible to their children, but since you are creating it afterwards they probably are not calling that method, a possible solution is to call the show method. void ChatWidget::displayChatAfterButtonPressed() { QLabel *lbl; lbl=new QLabel(this); lbl->setText(“Hello World 2”); lbl->show(); } comment: it seems strange … Read more