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 associate an QWidget object with QRect object so that whenever i want to know Qrect value i can get from Qwidget object?
Forum Updated to NodeBB v4.3 + New Features

How can we associate an QWidget object with QRect object so that whenever i want to know Qrect value i can get from Qwidget object?

Scheduled Pinned Locked Moved General and Desktop
19 Posts 4 Posters 7.7k 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    Here Qrect value will be provided from outside. I want to relate qrect so that knowing the widget object i can know the corresponding rect associated with that.

    Pratik Agrawal

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      What do you want to achieve?
      A widget already has an rectangle (where it is shown). Or do you need something else?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

        [quote author="Gerolf" date="1322036183"]What do you want to achieve?
        A widget already has an rectangle (where it is shown). Or do you need something else?[/quote]

        I don't want that rectangle. I want to keep extra info in a widget different from the rect of that widget so that i can access that info when i want.

        Pratik Agrawal

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

          /me is with Gerolf: what is it that you really want to achieve? Not how, but what? You keep opening topics that seem vaguely related. If I look at "your recent list of topics":http://developer.qt.nokia.com/forums/search_results/6e3f02d2e834553641c5de822d57bc85/, four of the top five of those seem related, at least at first sight.

          This rect that you are after, is that in any way related with the widget itself (as in: it represents its position), or is is just a piece of random data that you wish to associate with the widget? And that widget, can it be any of QWidget-derived class, or do you have a specific one in mind?

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

            [quote author="pratik041" date="1322037969"]
            [quote author="Gerolf" date="1322036183"]What do you want to achieve?
            A widget already has an rectangle (where it is shown). Or do you need something else?[/quote]

            I don't want that rectangle. I want to keep extra info in a widget different from the rect of that widget so that i can access that info when i want. [/quote]

            So... You have three routes:

            • If you have easy control over the kind of widget you wish to associate the data with, simply subclass it and add a property for this piece of data.
            • If subclassing isn't an option, because you need this to work for any QWidget derived class, you can either create a separate QHash<QWidget*, QRect> structure in which you keep the data, or
            • You can use QObject's dynamic properties functionality and call :
              @
              //set rect
              myWidget->setProperty("magicRectProperty", QRect(0,0,34,42));

            //retreive rect
            QRect r = myWidget->property("magicRectProperty").toRect();
            @

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

              [quote author="Andre" date="1322038217"]/me is with Gerolf: what is it that you really want to achieve? Not how, but what? You keep opening topics that seem vaguely related. If I look at "your recent list of topics":http://developer.qt.nokia.com/forums/search_results/6e3f02d2e834553641c5de822d57bc85/, four of the top five of those seem related, at least at first sight.

              This rect that you are after, is that in any way related with the widget itself (as in: it represents its position), or is is just a piece of random data that you wish to associate with the widget? And that widget, can it be any of QWidget-derived class, or do you have a specific one in mind? [/quote]

              It can be any Qwidget not specific and i want to associate a piece of random data.

              Pratik Agrawal

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                [quote author="pratik041" date="1322038513"]
                ... i want to associate a piece of random data.
                [/quote]

                You already got your answer then.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pratik041
                  wrote on last edited by
                  #8

                  [quote author="Andre" date="1322038471"]
                  [quote author="pratik041" date="1322037969"]
                  [quote author="Gerolf" date="1322036183"]What do you want to achieve?
                  A widget already has an rectangle (where it is shown). Or do you need something else?[/quote]

                  I don't want that rectangle. I want to keep extra info in a widget different from the rect of that widget so that i can access that info when i want. [/quote]

                  So... You have three routes:

                  • If you have easy control over the kind of widget you wish to associate the data with, simply subclass it and add a property for this piece of data.
                  • If subclassing isn't an option, because you need this to work for any QWidget derived class, you can either create a separate QHash<QWidget*, QRect> structure in which you keep the data, or
                  • You can use QObject's dynamic properties functionality and call :
                    @
                    //set rect
                    myWidget->setProperty("magicRectProperty", QRect(0,0,34,42));

                  //retreive rect
                  QRect r = myWidget->property("magicRectProperty").toRect();
                  @
                  [/quote]

                  In have tried it using the setproperty but one problem i am getting when i am passing "mywidget" object address to another function of different class and when their i tried to access the property of that object i did not get the desired QRect instead i got QRect(0,0,0,0);. what may be the problem?

                  Pratik Agrawal

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

                    There are many different options for what could go wrong, but you need to do your own debugging. There is no way can do that for you, especially if you don't provide us with the code you wrote for it.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      This can only be answered if we see more code.
                      Dynamic properties in general work, I use them from time to time.

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                        [quote author="Gerolf" date="1322040896"]This can only be answered if we see more code.
                        Dynamic properties in general work, I use them from time to time.[/quote]

                        I find it save to assume that Qt's functions work, unless I have some very strong proof they don't. That means that I assume the problem is in my code, not in Qt, and that works out reasonably well in debugging :-)

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          pratik041
                          wrote on last edited by
                          #12

                          Sorry, actually one mistake was there in my code now i am getting the proper output.One thing still i want to ask in the function in which i am using the following function
                          @QRect r = myWidget->property("magicRectProperty").toRect();@
                          i have only mywidget object not the string "magicRectproperty". so how can i get the rect using only object not the string?

                          Pratik Agrawal

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            lgeyer
                            wrote on last edited by
                            #13

                            The string is always the same, chosen by you - like a variable.

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              pratik041
                              wrote on last edited by
                              #14

                              [quote author="Lukas Geyer" date="1322042786"]The string is always the same, chosen by you. [/quote]

                              No my problem is the function in which i am using setproperty and property both are not in the same scope . So, when i want to retrieve the property of any object i dont know the string in that scope.so how i will get the rect?
                              @QRect r = myWidget->property( ? ).toRect();@

                              Pratik Agrawal

                              1 Reply Last reply
                              0
                              • L Offline
                                L Offline
                                lgeyer
                                wrote on last edited by
                                #15

                                You do know the string because you have chosen it.

                                If you assign a dynamic property of a QObject using setProperty("fancyPropertyName", fancyValue) it will be accessible anywhere in your application using property("fancyPropertyName"). Dynamic properties are just like member variables, but declared at runtime, not compile time.

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

                                  Lukas is totally right. You know the string in the same way that you know the name of class methods: you implemented them. On the other hand you can list them if you really want. See the QObject::dynamicPropertyNames() method.

                                  1 Reply Last reply
                                  0
                                  • P Offline
                                    P Offline
                                    pratik041
                                    wrote on last edited by
                                    #17

                                    [quote author="Lukas Geyer" date="1322043483"]You do know the string because you have chosen it.

                                    If you assign a dynamic property of a QObject using setProperty("fancyPropertyName", fancyValue) it will be accessible anywhere in your application using property("fancyPropertyName"). Dynamic properties are just like member variables, but declared at runtime, not compile time.[/quote]

                                    [quote author="Lukas Geyer" date="1322043483"]You do know the string because you have chosen it.

                                    If you assign a dynamic property of a QObject using setProperty("fancyPropertyName", fancyValue) it will be accessible anywhere in your application using property("fancyPropertyName"). Dynamic properties are just like member variables, but declared at runtime, not compile time.[/quote]

                                    Can you please see this
                                    @
                                    main.cpp
                                    int main(int argc, char *argv[])
                                    {
                                    QApplication a(argc, argv);
                                    QWidget w;
                                    ofi_vc_gui_tooltip tp(&w);
                                    QPushButton *button1 = new QPushButton(&w);
                                    QSlider *slider = new QSlider(&w);
                                    button1->setProperty ("button1",QRect(0,0,120,150));
                                    slider->setProperty ("slider",QRect(0,0,120,150));
                                    tp.add(button1);
                                    tp.add(slider);
                                    ..................................................
                                    ..................................................
                                    }

                                    customclass.cpp

                                    void customclass::add (QWidget *widget)
                                    {
                                    object = widget;
                                    qDebug ()<<object->property ( ? ).toRect ();
                                    // Here object can be anyone.

                                    }
                                    @
                                    Here object in custom class always different then how can i get the rect?

                                    Pratik Agrawal

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

                                      Why would you give the property the name of the object itself? What is that supposed to mean? Why not choose the same name in both cases, and choose a name that makes sense, like, "sensitiveRect" or something like that. Note that because the object instances are different, they properties will not overwrite each other or something like that. slider and button1 will just both have a property called "sensitiveRect".

                                      1 Reply Last reply
                                      0
                                      • P Offline
                                        P Offline
                                        pratik041
                                        wrote on last edited by
                                        #19

                                        Now i got thanks

                                        Pratik Agrawal

                                        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