What you are showing should be, at the very least, functions.
For example:
class Foo : public QWidget {
QScopedPointer<Ui::Foo> ui; // Don't use a raw pointer!
enum { LOC_04, LOC_END };
int m_editMode[LOC_END];
void lable1() { ui->lable->setText("NumbVal"); }
void lable2() { ui->lEditCaliCLFltBst->setText("UNDER PROCESS"); }
...
void f() {
...
if (EditMode[LOC_04]!=0) lable1(); else lable2();
...
}
}
With the little code you've shown, I infer that you have an interface that can be in various states, and those states are indicated through multiple user interface elements. This is what QStateMachine
is for.
The example below demonstrates the following:
The use of a state machine to control the appearance of the user interface in each state.
The user interface has two parallel states: the m_editState and the m_boldState. The states are parallel, meaning that the state machine is in both of those states at the same time. Imagine this was in a text editor of some sort.
The edit state can be in one of two substates: m_edit1 and m_edit2. Similarly, the bold state can be in two states: m_boldOn and m_boldOff.
Clicking the buttons switches the states, and modifies the indications on the labels.
Concise setup of a user interface without using the UI designer.
Direct use of QObject members in a QObject, without explicit heap storage. Note the absence of a single explicit new
and delete
in the entire code. This shouldn't be an end unto itself, but it certainly helps avoid some pitfalls of unmanaged pointers, and it halves the number of heap allocations per each object. This pattens also works great when you put all the members in a pimpl class.
A reasonably concise way of repeating some code for elements of a constant list, created in place. This is pre-C++11 code.
Referring back to your original code, perhaps the EditMode
could be represented by a set of states. If there are multiple aspects of the EditMode
, they'd be represented by parallel states - perhaps each entry in EditMode
would be a parallel state. Without knowing anything else about what you intend to achieve, it's hard to tell.

#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QStateMachine>
#include <QGridLayout>
class Widget : public QWidget {
QGridLayout m_layout;
QLabel m_label1, m_label2, m_label3;
QPushButton m_button1, m_button2, m_button3;
QStateMachine m_machine;
QState m_editState, m_boldState, m_edit1, m_edit2, m_boldOn, m_boldOff;
public:
Widget(QWidget * parent = 0) : QWidget(parent), m_layout(this),
m_label1("--"), m_label2("--"), m_label3("--"),
m_button1("Edit State 1"), m_button2("Edit State 2"), m_button3("Toggle Bold State"),
m_editState(&m_machine), m_boldState(&m_machine),
m_edit1(&m_editState), m_edit2(&m_editState),
m_boldOn(&m_boldState), m_boldOff(&m_boldState)
{
m_layout.addWidget(&m_label1, 0, 0);
m_layout.addWidget(&m_label2, 0, 1);
m_layout.addWidget(&m_label3, 0, 2);
m_layout.addWidget(&m_button1, 1, 0);
m_layout.addWidget(&m_button2, 1, 1);
m_layout.addWidget(&m_button3, 1, 2);
m_edit1.assignProperty(&m_label1, "text", "Edit State 1");
m_edit2.assignProperty(&m_label2, "text", "Edit State 2");
m_boldOn.assignProperty(&m_label3, "text", "Bold On");
m_boldOff.assignProperty(&m_label3, "text", "Bold Off");
m_editState.setInitialState(&m_edit1);
m_boldState.setInitialState(&m_boldOff);
foreach (QState * s, QList<QState*>() << &m_edit1 << &m_edit2) {
s->addTransition(&m_button1, SIGNAL(clicked()), &m_edit1);
s->addTransition(&m_button2, SIGNAL(clicked()), &m_edit2);
}
m_boldOn.addTransition(&m_button3, SIGNAL(clicked()), &m_boldOff);
m_boldOff.addTransition(&m_button3, SIGNAL(clicked()), &m_boldOn);
m_machine.setGlobalRestorePolicy(QState::RestoreProperties);
m_machine.setChildMode(QState::ParallelStates);
m_machine.start();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}