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. How can we set id to a widget in Qt?
Forum Updated to NodeBB v4.3 + New Features

How can we set id to a widget in Qt?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 16.4k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #6

    Just use a dynamic property then.

    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
      #7

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #8

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

        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
          #9

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #10

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

            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
              #11

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

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

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

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #13

                  Sure? Not even through widget promotion?

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

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

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

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #15

                      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.

                      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