如何将标签文本数据绑定到基础对象的属性? [英] How to data-bind a label text to an underlying object's property?

查看:25
本文介绍了如何将标签文本数据绑定到基础对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个将在嵌入式 ARM 设备上运行的 Qt4.8/C++ 应用程序(无 QML).该设备不断接收需要保存在对象中的大量数据点的值.现在,我想将 UI 元素绑定到此模型对象的特定属性,以便在 UI 中始终自动显示当前值和最新值.

We're building a Qt4.8/C++ application (no QML) that will run on an embedded ARM device. This device constantly receives values of a lot of datapoints that need to be held in an object. Now I want to bind UI elements to specific properties of this model object in order to automatically always show the current and up-to-date values in the UI.

Qt 中有我可以使用的机制吗?或者我是否必须跟踪任何更改并手动更新 UI?如果有人能给我一个非常基本的示例,例如如何将标签文本数据绑定到对象的双值属性,那就太好了.提前致谢!

Is there a mechanism in Qt that I can use? Or do I have to keep track of any changes and manually update the UI? It would be great if someone could give me a very basic example on how to, for example, data-bind a label text to a double value property of an object. Thanks in advance!

推荐答案

这是我的解决方案.假设您有一些数据接收器类和数据源标识符:

Here is my solution. Let's assume you have some data receiver class and identifiers for your data sources:

enum SourceIds
{
    SourceCamera,
    SourcePort,
    SourceMagic
}

class DataReceiver : public QObject
{
    Q_OBJECT

public:
    DataReceiver(QObject *parent = 0);

    void updateValue(int sourceId, const QVariant &value);

signals:
    void valueChanged(int sourceId, const QVariant &newValue);

private:
    QHash<int, QVariant> data;
};

void DataReceiver::updateValue(int sourceId, const QVariant &value)
{
    QVariant oldValue = data.value(sourceId);

    data[sourceId] = value;

    if (oldValue != value)
    {
        emit valueChanged(sourceId, value);
    }
}

然后你可以创建一个数据映射器类来监听你的接收器 valueChanged 信号:

Then you can create a data mapper class that will listen to your receiver valueChanged signal:

class DataMapper : public QObject
{
    Q_OBJECT

public:
    DataMapper(QObject *parent = 0);
    void registerLabel(int sourceId, QLabel *label);

public slots:
    void updateLabel(int sourceId, const QVariant &value);

private:
    QHash<int, QLabel*> labels;
};

void DataMapper::registerLabel(int sourceId, QLabel *label)
{
   labels[sourceId] = label;
}

void DataMapper::updateLabel(int sourceId, const QVariant &value)
{
   QLabel *label = labels.value(sourceId, NULL);
   if (label != NULL)
   {
       label->setText(value.toString());
   }
}

您创建一个接收器和一个映射器对象并将它们连接起来:

You create a receiver and a mapper objects and connect them:

DataReceiver *receiver = new DataReceiver;

QLabel *cameraLabel = new QLabel;
QLabel *portLabel = new QLabel;
QLabel *magicLabel = new QLabel;

DataMapper *mapper = new DataMapper;
mapper->registerLabel(SourceCamera, cameraLabel);
mapper->registerLabel(SourcePort, portLabel);
mapper->registerLabel(SourceMagic, magicLabel);

connect(receiver, SIGNAL(valueChanged(int, QVariant)), mapper, SLOT(updateLabel(int, QVariant)));

希望这对你有用.

这篇关于如何将标签文本数据绑定到基础对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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