How to set current widget from QWidget class
-
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
-
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 ) -
@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 :)
-
@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