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?



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



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



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



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



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

    You already got your answer then.



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



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



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



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



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



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



  • [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();@



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



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



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



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



  • Now i got thanks


Log in to reply
 

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