How can we set id to a widget in Qt?
-
wrote on 23 Feb 2012, 06:46 last edited by
I have checked the documentation but not found any function to set id to a widget. Is their any function to set id to a widget.
-
wrote on 23 Feb 2012, 06:52 last edited by
What do you mean with 'id'? Some kind of number? Perhaps you can use the objectName? Or you could use a dynamic property?
-
wrote on 23 Feb 2012, 06:55 last edited by
You mean like a name?
QString accessibleName () const
void setAccessibleName ( const QString & name ) -
wrote on 23 Feb 2012, 07:02 last edited by
[quote author="ddriver" date="1329980110"]You mean like a name?
QString accessibleName () const
void setAccessibleName ( const QString & name )[/quote]
That property is not meant for program-internal identifiers. It is meant for devices like screen readers or braille devices. -
wrote on 23 Feb 2012, 07:02 last edited by
[quote author="Andre" date="1329979961"]What do you mean with 'id'? Some kind of number? Perhaps you can use the objectName? Or you could use a dynamic property?[/quote]
By id i mean some number.
-
wrote on 23 Feb 2012, 07:08 last edited by
Just use a dynamic property then.
-
wrote on 23 Feb 2012, 07:08 last edited by
Andre - I see...
pratik041 - what is wrong with adding an int member variable and setter/getter methods to your custom widget? It doesn't seem like QWidget has this implemented by default.
-
wrote on 23 Feb 2012, 07:14 last edited by
[quote author="ddriver" date="1329980939"]Andre - I see...
pratik041 - what is wrong with adding an int member variable and setter/getter methods to your custom widget? It doesn't seem like QWidget has this implemented by default.[/quote]
Nothing wrong with that approach, if you only need it for a specific or at least a limited number of classes. If you need it for a whole range of different widgets, I would want to use something in the base class, hence the dynamic property recommendation. However, you are right, adding a property with getters and setters is the better way in principle. -
wrote on 23 Feb 2012, 07:25 last edited by
You can create a class IDWidget, inherit QWidget, add the extra functionality, and inherit from IDWidget instead of QWidget if multiple instances of widgets with ID are needed, in order to save re-implementing the ID for each and every one.
However there is a limitation if you want to use stock widgets that inherit from QWidget, in which case Andre's solution is more appropriate.
-
wrote on 23 Feb 2012, 07:33 last edited by
If we're throwing around solutions anyway: you could even create a class template that adds this function to any widget you want, including all stock widgets. :-)
@
template <class T>
public IDClass<T>: public T {
public:
IDClass(int id, QWidget* parent = 0):
m_id(id),
T(parent)
{}void setId(int newId) {m_id = newId};
int id() const {return m_id;}private:
int m_id;
}typedef IDClass<QLineEdit> IdLineEdit;
typedef IDClass<QPushButton> IdPushButton;
//etc.@
Of course: brain to terminal, I did not test this pseudo code :-)
-
wrote on 23 Feb 2012, 07:35 last edited by
Yes, thanks, the more the better :) It is a good solution indeed.
-
wrote on 23 Feb 2012, 13:28 last edited by
Sure? Not even through widget promotion?
-
wrote on 23 Feb 2012, 13:34 last edited by
Promotion can work, yes. But I don't know if the special editors remain, for example for a QTextEdit.
-
wrote on 23 Feb 2012, 13:38 last edited by
They do. Just make sure you use the right base widget when promoting. Promote a QTextExit to an IdTextEdit and you're good to go. However, that will of course still not allow you to set the ID from designer directly. I did not read this requirement from the question, so I guess it would not be needed.
7/15