How can we set id to a widget in Qt?



  • 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.



  • What do you mean with 'id'? Some kind of number? Perhaps you can use the objectName? Or you could use a dynamic property?



  • You mean like a name?

    QString accessibleName () const
    void setAccessibleName ( const QString & name )



  • [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.



  • [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.



  • Just use a dynamic property then.



  • 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 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.



  • 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.



  • 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 :-)



  • Yes, thanks, the more the better :) It is a good solution indeed.



  • @Andre: But you can't use them in Qt Designer unless you're preparing a whole new set of plugins :)



  • Sure? Not even through widget promotion?



  • Promotion can work, yes. But I don't know if the special editors remain, for example for a QTextEdit.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.