How to use QWidget from a class "B" in QStackedWidget in class "A"



  • Hello. Hope you can help me with my little problem.

    I have created a QWidget in class "B", now I want to display that QWidget in a class "A" within a QStackedWidget.

    Can you please give me an example how to do this.

    Thank yout



  • Hi,

    I don't understand your question, can you please rephrase it?

    I make a guess:

    You implemented a class B (which is derived from QWidget).

    You implement a class A which is also derived from QWidget and which has a child QStackedWidget.

    you want to show an instance of B inside this stacked widget.

    Is that right?



  • @
    class ICreator {
    public:
    virtual QWidget* factoryMethod() = 0;
    };

    class ConcreteCreator : public ICreator {
    public:
    QWidget* factoryMethod() {
    return new QWidget;
    }
    };

    class Client
    {
    private:
    QStackedWidget *stack;
    ConcreteCreator *creator;

        void addToStack()
        {
            QWidget* w = creator->factoryMethod();
    
            if ( !w ) //like { qDebug() << "Creator can not create widget"; return; }
    
            stack->addWidget(w); // <-- you can see after that qDebug() << w->parent(); will be
    

    //stack you dont need delete w;
    }
    }

    @

    p.s. See more factory method or creational patterns.



  • [quote author="Gerolf" date="1327334164"]Hi,

    I don't understand your question, can you please rephrase it?

    I make a guess:

    You implemented a class B (which is derived from QWidget).

    You implement a class A which is also derived from QWidget and which has a child QStackedWidget.

    you want to show an instance of B inside this stacked widget.

    Is that right?[/quote]

    Yes that is rigth.



  • Oo...you just need read some book about OOP.

    @
    #include "B.h"

    class A : public QWidget {
    private:
    //First impl if you want to know about b all time;
    B *b; //or B b; -- if you don't want to dynamically create instance of B;

         QStackedLayout *stack;   //your stack
    
     public:
        A() : b(new B()), stack(new QStackedLayout(this)) {}
    
        void foo()
        {
            stack->addWidget(b);
    
            //or second impl if you don't want to know about b all time
            stack->addWidget(new B());
        }
    

    };
    @


Log in to reply
 

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