Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QWidget from a class "B" in QStackedWidget in class "A"
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    depecheSoul
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stima_ua
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          depecheSoul
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stima_ua
            wrote on last edited by
            #5

            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());
                }
            

            };
            @

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved