Important: Please read the Qt Code of Conduct -

Finding object from different object of the same class

  • I have sub-classed QWidget class and named it MYQWidget.
    I added 4 QWidget in my interface and promoted those QWidget from to my class MyWidget.

    I need to change background color of each widget. How can I identify an object and perform a the drawing in different colors or how can I do this?


  • Lifetime Qt Champion


    There are several possibilities. You can e.g. have a Q_PROPERTY with that color and set it from the editor.

    Hope it helps

  • Thanks for your answer. It is not clear to me. Could you please post a small example?

  • This might help please check the link

  • Hi,

    is your question actually how you can set the background colour of a QWidget(-subclass)? Because the thread title seems to be pointing more in the direction, how you could find all instances of you class, so you can set background colour for all of them.

    If that is your actual question: There is no Qt-mechanism I'm aware of that could do this. However you can implement such a mechanism pretty easily. For instance by adding a class-member QList<MyWidget*> and in the constructor of you class, add a pointer to the instance to that list.

    I didn't test s this code, but it would look similar to this:
    class MyWidget : public QWidget{
    static QList<MyWidget*> instances;
    MyWidget(QWidget* parent=0);


    MyWidget::MyWidget(QWidget* parent) : QWidget(parent){


Log in to reply