0
votes

I have multiple QLineEdit widgets on a configuration widget.

When the configuration text is changed I want to store the updated value (there are a few of them so I don't want to pass them in one function call at the end).

I was thinking to connect them to one slot or go through QSignalMapper to one slot.

However I'm having trouble finding an elegant way to tell the QLineEdits apart, I want to know which QLineEdit emitted the signal - since I'm in handler object that implements the logic I can't compare the sender() with the original object.

I can hook them up through QSignalMapper so that my handler gets either int id or QObject* or QWidget* reference to the actual widget that emitted the signal so I can get easily the new text, but I would like to be able to switch on some id to update the correct setting internally.

Is there any elegant way to tag or label Qt objects or widgets?

3
I don't understand why you can't use sender() - could you clarify?Mat
If the event is handled in model it does not have reference to the original object so I can't compare if sender() is my serverLineEdit or other widget - it would be nice to be able to get some ID/tag/label that I could check (instead of objectName() ...). But maybe I'm not doing it correctly ... I could just send all values across to model when the apply button is pressed ... but I was looking for something more elegantstefanB

3 Answers

2
votes

You may rewrite your own QLineEdit with a value "int id". And whenever QLineEdit emits a signal, also emit your own signal with parameter "id".

class MyLineEdit : public QLineEdit
{
   Q_OBJECT
   private: int id;
   public:  int getID() { return id; }
            void setID(int _id) { id = _id; }
   signals: void myTextEdited(QString,int);
   slots:   void emitMyTextEdited(QString text) { emit myTextEdited(text,id); }
   public:  MyLineEdit(QWidget *parent = 0) : QLineEdit(parent)
            {
                connect(this,SIGNAL(textEdited(QString)),this,SLOT(emitMyTextEdited(QString)));
            }
};

Here, if you want to emit the textEdited signal with the identity of the object, you can rewrite textEdited signal with your way.

1
votes

Dynamic properties allow you to 'tag' extra data at run-time onto any QObject derived class. See the QObject::setProperty documentation.

I personally don't like using dynamic properties as they aren't documented in the header of the class. I prefer to inherit from the class and add a standard property.

1
votes

Hook them up through QSignalMapper, with a mapping type of QObject*. You can define something like this as your QObject:

class MyQLineEditData : public QObject {
  Q_OBJECT
public:
  QLineEdit* lineEdit ;
  int id ;
} ;