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 set current widget from QWidget class
Forum Update on Monday, May 27th 2025

How to set current widget from QWidget class

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.6k Views
  • 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.
  • A Offline
    A Offline
    AaronKelsey
    wrote on last edited by
    #1

    I have created a QStackedWidget object in my main window which I have used to add my custom widgets classes to using the addWidget function. The current page (widget) has been set using setCurrentWidget and has been done in the main window class constructor.

    The issue I have is that I am unable to call setCurrentWidget from page1, to then set the current page to page2 as I do not have access to pStackedWidget in my class 'PageOne'.

    I am sorry if my explanation is unclear and please feel free for ask for more information. I am not using Qt Designer and the UI and logic is all being written therefore I cannot use ui->pStackedWidget.

    Below is a copy of the relevant code.

    Thank you in advance

    Main window (CertTool.cpp)

    #include "certtool.h"
    #include "pageone.h"
    #include "pagetwo.h"
    #include "pagethree.h"
    #include <QHBoxLayout>
    #include <QStackedWidget>
    
    CertTool::CertTool() :
              QMainWindow()
    {
      pPageOne = new PageOne(this);
      pPageTwo = new PageTwo(this);
      pPageThree = new PageThree(this);
    
      pStackedWidget = new QStackedWidget(this);
      pStackedWidget->addWidget(pPageOne);
      pStackedWidget->addWidget(pPageTwo);
      pStackedWidget->addWidget(pPageThree);
      pStackedWidget->setCurrentWidget(pPageOne);
    
      pLayout = new QHBoxLayout(this);
      pLayout->addWidget(pStackedWidget);
      setLayout(pLayout);
    }
    

    Main Window (CertTool.h)

    #ifndef CERTTOOL_H
    #define CERTTOOL_H
    
    #include <QMainWindow>
    #include <QStackedWidget>
    #include <QHBoxLayout>
    #include "pageone.h"
    #include "pagetwo.h"
    #include "pagethree.h"
    
    class CertTool : public QMainWindow
    {
      Q_OBJECT
    
    public:
      CertTool();
    
    private:
      PageOne* pPageOne;
      PageTwo* pPageTwo;
      PageThree* pPageThree;
    
      QStackedWidget* pStackedWidget;
      QHBoxLayout* pLayout;
    
    };
    
    #endif // CERTTOOL_H
    
    

    pageone.cpp

    #include "pageone.h"
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QPushButton>
    
    PageOne::PageOne(QWidget *pParent) :
             QWidget(pParent)
    {
      QHBoxLayout *pLayout = new QHBoxLayout(this);
      setLayout(pLayout);
    
      pParent->window()->setWindowTitle("Page One");
      pParent->window()->setMinimumSize(600,450);
    
      pButton = new QPushButton(tr("Click here"), this);
      connect(pButton, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
    
      pLayout->addWidget(pButton);
    
    
    }
    
    void PageOne::on_button_clicked()
    {
      // I want to set current widget here to Page Two
    }
    
    

    pageone.h

    #ifndef SSLCERTIFICATE_PAGEONE_H
    #define SSLCERTIFICATE_PAGEONE_H
    
    #include <QWidget>
    #include <QPushButton>
    
    class PageOne : public QWidget
    {
      Q_OBJECT
    
    public:
      PageOne(QWidget *pParent = nullptr);
    
    private slots:
      void on_button_clicked();
    
    private:
      QPushButton *pButton;
    
    };
    
    #endif // SSLCERTIFICATE_PAGEONE_H
    
    
    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums
      You can use signals and slot for that.
      in CertTool
      you define some slot
      like
      void PageNext()
      void PagePrev()
      void setPage(int index) ;

      then in PageOne/PageTwo class
      define a matching signal.
      and simply emit it from with in the class to have CertTool
      flip between pages without pages have to know anything about CertTool.
      (note - you shall connect signal to slots when you create the pages )

      A 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi and welcome to the forums
        You can use signals and slot for that.
        in CertTool
        you define some slot
        like
        void PageNext()
        void PagePrev()
        void setPage(int index) ;

        then in PageOne/PageTwo class
        define a matching signal.
        and simply emit it from with in the class to have CertTool
        flip between pages without pages have to know anything about CertTool.
        (note - you shall connect signal to slots when you create the pages )

        A Offline
        A Offline
        AaronKelsey
        wrote on last edited by
        #3

        @mrjj Thank you for your prompt response. I have defined the signals and slots you stated. Unfortunately I have little experience with signals and slots, how would I emit it to change the page?

        mrjjM jsulmJ 2 Replies Last reply
        1
        • A AaronKelsey

          @mrjj Thank you for your prompt response. I have defined the signals and slots you stated. Unfortunately I have little experience with signals and slots, how would I emit it to change the page?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AaronKelsey
          also make sure you have the Q_OBJECT
          class Page: public QWidget
          {
          Q_OBJECT
          ..

          and then simply do
          emit SignalName()

          if it has a parameter like int so
          emit SignalName(37)

          and thats it :)

          1 Reply Last reply
          3
          • A AaronKelsey

            @mrjj Thank you for your prompt response. I have defined the signals and slots you stated. Unfortunately I have little experience with signals and slots, how would I emit it to change the page?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @AaronKelsey said in How to set current widget from QWidget class:

            Unfortunately I have little experience with signals and slots

            I suggest to learn this core concept of Qt, see: http://doc.qt.io/qt-5/signalsandslots.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

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