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

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

问题描述

我们正在建设一个Qt4.8 / C ++应用程序(不QML),将嵌入式ARM设备上运行。此装置持续接收大量需要在一个目的是要保持数据点的值。现在我要绑定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 信号:

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

我希望这将是有用的为您服务。

I hope this will be usefull for you.

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

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