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?

    Regards,


  • Lifetime Qt Champion

    Hi,

    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
    http://www.informit.com/articles/article.aspx?p=1405227



  • 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{
    private:
    static QList<MyWidget*> instances;
    public:
    MyWidget(QWidget* parent=0);
    ~MyWidget();
    };
    @

    and

    @
    MyWidget::MyWidget(QWidget* parent) : QWidget(parent){
    instances.append(this);
    }

    MyWidget::~MyWidget(){
    instances.removeOne(this);
    }
    @


Log in to reply
 

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