23

I would like to set an integer number to be stored in a QWidget, and I think the setUserData member function would do the trick but I can't find any documentation. Any hints?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
  • 3
    QObject::setUserData is marked as "internal" -- it's probably advisable to avoid using it. – RA. Oct 22 '12 at 20:14

2 Answers2

33

You might be looking for QObject::setProperty() (which is of course inherited by QWidget).

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Is it possible to assign an object (say of class Foo) as a property and get it back when queried, instead of using just strings, ints or other simple data type properties? – SexyBeast Feb 25 '15 at 17:18
  • 1
    I am not sure as I am not a QT expert. But the solution I posted below of creating a inherited type would certainly work for an arbitrary object. You would simple replace "Data" with "Foo". – Alexander Van Atta Feb 26 '15 at 16:17
  • @SexyBeast YES it is possible and makes all this very useful: you can have QVariant support arbitrary classes, as described here: https://doc.qt.io/qt-5/custom-types.html (with a nice example here: https://doc.qt.io/qt-5/qtcore-tools-customtype-example.html) – Francois Bertrand May 06 '22 at 16:29
11

I am not an expert in QT but why not just create a class that inherits from QWidget and has an integer? Like so:

class MyDerivedWidget : public QWidget
{
    public:
  MyDerivedWidget();

    private:
  Data *myUserData;
}; 

Or if you insist on using the setUserData checkout the last post here.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Alexander Van Atta
  • 870
  • 11
  • 34
  • It is, if you do explicit MyDerivedWidget(QWidget parent=0){}, simple and effective, still, it sounds a bit hacky to me, but useful answer so I voted up, thx. – Barney Szabolcs Oct 22 '12 at 20:17
  • IMO, this is the least hacky of the two answers. If you want to store data in a QWidget (vs just using a normal struct), the proper way would be to Inherit the class you want, add more functionality and do it 'right'. Atleast by inheriting, you'd be able to add accessors and the such to make it more useful than just passing a standard QWidget around with an additional random property... – g19fanatic Oct 22 '12 at 20:31
  • 9
    @g19fanatic, respectfully disagree (and apparently the library designers are on my side, otherwise `setProperty()` wouldn't exist). Sometimes it can be very convenient to have a simple "bag of data" associated with an instance, without having to design and maintain a whole subclass just to get that. – Frédéric Hamidi Oct 22 '12 at 20:33
  • 1
    @Barnabas I agree with g19fanatic inheritance is not "hacky" it is a fundamental feature of object-oriented programming. If you do not feel comfortable with inheritance I would strongly recommend looking into it and learning more. It is a very, very powerful tool. – Alexander Van Atta Oct 22 '12 at 20:39
  • @frederic Yeah I would agree with that as long as the "bag of data" remains simple. If you are looking to add more then just a few primitvies though I think you should start thinking about inheritence. – Alexander Van Atta Oct 22 '12 at 20:43
  • 1
    @vannattab, thanks for your answer, let's not break the bits here. I did not mean to say too much, "hacky" could have been a bit strong. – Barney Szabolcs Oct 22 '12 at 21:08
  • setUserData is necessary because I have "class PieView : public QAbstractItemView" to which the compiler is refusing to let me add my own functions. – Pierre Feb 04 '20 at 20:33