简体   繁体   English

更改哈希图的Qt信号

[英]Qt signal for changed hashmap

I've got a hashmap ReferenceMap in a Qt serial terminal program. 我在Qt串行终端程序中有一个hashmap ReferenceMap

The program receives a key to ReferenceMap in form of a reference followed by a value. 该程序以引用的形式接收引用映射的键,后跟一个值。 It then uses the Qt hashmap insert function to insert the value. 然后,它使用Qt hashmap insert函数插入值。 It's very neat and I would like to keep that part of the program. 这非常整洁,我想保留该部分程序。

The problem is that I would like to to do different things when different new values are coming in. I therefore would like to emit a signal when a value associated with a special place in the referencemap is changed. 问题在于,当传入不同的新值时,我想做不同的事情。因此,当与参考图中特殊位置关联的值发生变化时,我想发出一个信号。

According to the guide below, an object that emits a signal can easily be created. 根据以下指南,可以轻松创建发射信号的对象。 http://qt-project.org/doc/qt-4.8/signalsandslots.html http://qt-project.org/doc/qt-4.8/signalsandslots.html

I think I got a solution, however, and want to discuss wether it's a neat way or not. 我想我找到了一个解决方案,但是想讨论一下这是否是一种巧妙的方法。

I could create a class in like this 我可以这样创建一个类

class Sfloat : public QObject
{
    Q_OBJECT
public:
    explicit Sfloat(QObject *parent = 0);

    float Get();
    void SetValue(float value);

private:
float MyFloat;


signals:
void ValueChanged(float newValue);

public slots:

};

The ReferenceMap could then be set to contain references to SFloat object like, 然后可以将ReferenceMap设置为包含对SFloat对象的引用,例如,

ReferenceMap(Key, &SFloat). ReferenceMap(Key,&SFloat)。

When I have the key, I could get the object reference, call SetValue() and a signal would be emitted. 当有了键时,我可以获取对象引用,调用SetValue(),然后会发出信号。 However, is this the best way to solve it? 但是,这是解决问题的最佳方法吗?

I don't think that's a good idea. 我认为这不是一个好主意。 You are wrapping every float of your hash with a QObject which sounds very heavy to me. 您正在用QObject包裹哈希表的每个浮动,这对我来说听起来很沉重。

You can hap the whole HashMap inside the QObject and emit the signal from there when the value you are interested in has changed. 您可以将整个HashMap放置在QObject中,并在您感兴趣的值更改时从那里发出信号。

class MyHashMap : public QObject
{
    Q_OBJECT
public:
    explicit MyHashMap(QObject *parent = 0);

    float Get(const QString& key) const;
    void SetValue(const QString& key, float value)
    {
      // only emit value changed if the value has actually changed
            QHashMap<QString, float>::iterator it = myHashMap.find(key);
            if(it != myHashMap.end()){
                if(it.value != value){
                    myHashMap[key] = value;
                    emit ValueChanged(key, value);
                }
            }
    }

private:
QHashMap<QString, float> myHashMap;

signals:
void ValueChanged(const QString& key, float newValue);
};

According to your comment, you want to update a label when a value has changed. 根据您的评论,您想在值更改后更新标签。 Simply add all the labels to a QHashMap, then connect the ValueChanged signal to a slot that updates the right label: 只需将所有标签添加到QHashMap,然后将ValueChanged信号连接到更新正确标签的插槽即可:

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0)
        {
            ...
            ...
            myLabels["label1"] = myLabel1;
            myLabels["label2"] = myLabel2;
            QObject::connect(myLabels, SIGNAL(ValueChanged(const QString&, float)), 
                this, OnValueChanged(const QString&, float));
        }

public slots:
    void OnValueChanged(const QString& key, float newValue){
        QHashMap<QString, QLabel*>::iterator it = myLabels.find(key);
        if(it != myLabels.end()){
            it.value()->setText(QString::number(newValue));
        }
    }

private:
QHashMap<QString, QLabel*> myLabels;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM