我们如何从结构中访问#define变量 [英] How we can access #define variable from structure

查看:246
本文介绍了我们如何从结构中访问#define变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已为我想从结构访问的成员定义了宏。我不想将类型转换为任何其他数据类型。

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.

示例:

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

我想从结构中访问这个LABLE变量。但是如果我有更大数量的EditMode数组被引入 - 我不能让我的程序lenthy我只是想通过他们通过一个结构访问。

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.

推荐答案

您显示的内容至少应该是功能。

What you are showing should be, at the very least, functions.

例如:

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

你已经显示,我推断你有一个接口可以处于各种状态,这些状态通过多个用户界面元素指示。这是 QStateMachine 的用途。

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. 使用状态机控制每个状态下用户界面的外观。

  1. The use of a state machine to control the appearance of the user interface in each state.

用户界面有两个并行状态:m_editState和m_boldState。状态是并行的,意味着状态机同时处于这些状态的两者中。想象这是在某种文本编辑器中。

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.

编辑状态可以是两个子状态之一:m_edit1和m_edit2。类似地,粗体状态可以处于两种状态:m_boldOn和m_boldOff。

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.

在不使用UI设计器的情况下简化用户界面设置。

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

QObject,没有显式堆存储。注意,在整个代码中没有单个显式的 new delete 。这不应该是一个结束本身,但它肯定有助于避免非托管指针的一些陷阱,它减半每个对象的堆分配的数量。当所有成员都加入 pimpl类。

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.

对一个常量列表的元素重复一些代码的合理简洁的方法。这是前C ++ 11代码。

A reasonably concise way of repeating some code for elements of a constant list, created in place. This is pre-C++11 code.

参考你的原始代码,也许 EditMode 可以由一组状态表示。如果 EditMode 的多个方面,它们将由并行状态表示 - 也许 EditMode 中的每个条目成为平行状态。

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

这篇关于我们如何从结构中访问#define变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆