Does Qt controls have a property to store data that is closely associated with the control?



  • I am developing an application with controls that has a data structure associated to it. Is there a generic property for Qt control that I can set the data to? Something similar to VS .net´s control.Tag property.

    thanks in advance!



  • The Meta-Object system and Q_PROPERTY .. something that you can use?



  • Not quite what I am looking for. I was hoping for something like this...

    // Data Associated Class
    class MyObject : public QObject
    {
    public:
    QString Name;
    QString Address;
    int value;

    QString ToHex(int value);
    QString ToBinary(int value);
    

    }

    //Main form calling function
    void OnButtonClicked()
    {
    MyObject* obj = (MyObject*)txt_Value->Tag; // Does Qt contain something like this object

    if (HexFlg)
        txt_Value.SetText(obj->ToHex((int)txt_Value->Text());
    else
        txt_Value.SetText(obj->ToBinary((int)txt_Value->Text());
    

    }

    Thanks!



  • can you markup the code pls, using @ tag



  • Sorry, I did not realize I can do the following....

    @// Data Associated Class
    class MyObject : public QObject
    {
    public: // Variables
    QString Name;
    QString Address;
    int value;

    // public methods
    QString ToHex(int value); 
    QString ToBinary(int value);
    

    }

    //Main form calling function
    void OnButtonClicked()
    {
    // assign object to the control associated data (Does Qt control contain such object???)
    MyObject* obj = (MyObject*)txt_Value->Tag;

    if (HexFlg) 
        txt_Value.SetText(obj->ToHex((int)txt_Value->Text()); 
    else 
        txt_Value.SetText(obj->ToBinary((int)txt_Value->Text());
    

    } @



  • bq. Sorry, I did not realize I can do the following…

    I meant I did not know I can format my code in such manner. I am still curious if Qt controls contain a generic object property so I can assign my associated data to it... Sorry for any confusion.



  • What are Name and Address in MyObject for? They are not used!

    What type/class is txt_Value?

    Do you really want to just cast any text you get with txt_Value->Text() to an int without checking if that's possible?

    From what I guess it to be, I'd extend the Qt Widget and handle the signals internally and put the computed result into a Q_PROPERTY that can read afterwards.





  • The code snippet is just a sample of what I would like to do. I have not added any error checking or such.

    bq. What type/class is txt_Value?

    txt_Value is a lineedit widget.

    My main objective is to have all my widget assigned to its associated data class in the constructor so that I can cast the widget generic QObject property (if it exist) when a certain signal is received. I understand that I can create a custom class inherited from any of Qt widget and add a Q_PROPERTY that I can set. I am just wondering if Qt´s basic controls such as combobox, line edit, label, etc.. already contain this and that I have overlooked something.



  • Each Qt widget is derived from QObject. That means you can use the @setProperty()@ function of each widget to dynamically assign any data to the widget. Later you can read out the data with the @property()@ function of the widget.

    You wil find more information here "http://doc.qt.nokia.com/4.7/qobject.html#setProperty":http://doc.qt.nokia.com/4.7/qobject.html#setProperty



  • thank you guys for your response!!! you guys were really helpful in pointing out to me that I can dynamically create a property for my widgets.



  • Some of the Qt Widgets support already associating some data with it (e.g. QComboBox can associate a QVariant value to each of the choices you have, see "http://doc.qt.nokia.com/4.7/qcombobox.html#addItem":http://doc.qt.nokia.com/4.7/qcombobox.html#addItem


Log in to reply
 

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