Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Does Qt controls have a property to store data that is closely associated with the control?

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

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 6.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phamtv
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          can you markup the code pls, using @ tag

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phamtv
            wrote on last edited by
            #5

            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());
            

            } @

            1 Reply Last reply
            0
            • P Offline
              P Offline
              phamtv
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  you can find more info "here":http://doc.trolltech.com/4.7-snapshot/properties.html#qt-s-property-system

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    phamtv
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      Uwe Kindler
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        phamtv
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          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

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved