0

I have defined macros for members that I want to access from a structure. I don't want to type cast to any another data type.

Example:

#define LABLE ui->lable->setText("NumbVal")
#define LABLE1 ui->lEditCaliCLFltBst->setText("UNDER PROCESS") 
if (EditMode[LOC_04]!=0) { LABLE; } else { LABLE1; } 

I want to access this LABLE variable from a structure. But what if I have a larger number of EditMode array entried - I can't make my program lenthy I just want to access through them through a structure.

Kuba hasn't forgotten Monica
  • 88,505
  • 13
  • 129
  • 275
P Kumar
  • 85
  • 2
  • 11
  • Can you show a better example of what you mean by accessing something from a structure? – Retired Ninja Jul 25 '14 at 05:59
  • 2
    your macro is a function call, not a variable. – UmNyobe Jul 25 '14 at 05:59
  • Actually, `LABLE;` should work, given that `ui` is defined in the structure at the current scope and has a `lable` attribute which also has a `setText` method. – Frederik.L Jul 25 '14 at 06:03
  • @Retired Ninja- lable is tag name of lable that can point to setText(); fuction - I am using this LABLE where I want to display; let say : – P Kumar Jul 25 '14 at 07:27
  • 2
    trash that macro, it is terrible, doesn't help and whoever write such macro deserves butt kicking. End this ending comment makes this macro completely useless. – Marek R Jul 25 '14 at 08:05
  • Don't tell us *how* you want to do something. Tell us *what* you want to do and *why*. I imagine that you have some user interface elements, and want to do some actions across many of them. You **must** let us know what your goal is. We can't read your mind. Your phrase "accessing x from a structure" is meaningless. It means something to you, but nothing to us. You must explain it so that someone who knows *nothing* about your project will be able to understand it. – Kuba hasn't forgotten Monica Jul 25 '14 at 09:29
  • 1
    An important distinction: `#define` macros are *not* variables. They are essentially textual "search-replace" operations (especially macros without parameters). The macro is replaced with actual C++ code, as text modification, before actual C++ compilation starts. – hyde Jul 25 '14 at 19:29

1 Answers1

2

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:

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

  2. Concise setup of a user interface without using the UI designer.

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

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

screenshot

#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();
}
Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 88,505
  • 13
  • 129
  • 275