Reputation: 129
Hi everyone I'm pretty new to programming with Qt and I would like to create a widget using a QStackedLayout
. I already designed some widgets with Qt Creator, added them to the QStackedLayout
and set it to the main widget. But now I would like to change the widgets using buttons inside the added widgets using the setCurrentIndex
method. Now I have to use the connect
function but in the main widget class I can't access components from the other widgets to connect them. So how can I do this?
#include "mainwindowwidget.h"
#include "ui_mainwindowwidget.h"
MainWindowWidget::MainWindowWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindowWidget)
{
qApp->setStyleSheet("MainWindowWidget {background-color : red}");
//initializing widgets
this->mainWidget_ = new MainWidget;
this->createGameWidget_ = new CreateGameWidget;
this->widgets_ = new QStackedLayout;
//adding widgets to QstackedLayout
this->widgets_->addWidget(this->mainWidget_);
this->widgets_->addWidget(this->createGameWidget_);
this->setLayout(this->widgets_);
this->showFullScreen();
// I would like to connect the qstackedlayout
// = widgets_ with a button placed in mainwidget_
ui->setupUi(this);
}
MainWindowWidget::~MainWindowWidget()
{
delete ui;
}
Upvotes: 3
Views: 5037
Reputation: 12901
You have a few options here. If your button is a public member of MainWidget
, then you can simply connect the button's clicked()
signal to your slot in MainWindow
.
//mainwindow.h
...
public slots:
void buttonClicked();
//mainwindow.cpp
...
connect(mainWidget_->button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
...
void buttonClicked()
{
//do what you want to do here...
}
Another option is to create a custom signal in your MainWidget
class. Then connect your button's clicked()
signal to this custom signal:
//mainwidget.h
...
signals:
void buttonClickedSignal();
//mainwidget.cpp
connect(button, SIGNAL(clicked()), this, SIGNAL(buttonClickedSignal()));
Then connect your buttonClickedSignal()
signal to a slot in your MainWindow
:
//mainwindow.cpp
connect(mainWidget_, SIGNAL(buttonClickedSignal()), this, SLOT(buttonClicked()));
A third option would be to add a function to your MainWidget
class, that returns a pointer to your button. Then call this function in your MainWindow
class, and use that pointer to connect your button to a slot.
//mainwidget.h
...
public:
QPushButton* getButton();
...
//mainwdiget.cpp
...
QPushButton* getButton()
{
return button;
}
...
//mainwindow.cpp
...
QPushButton *button = mainWidget_->getButton();
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
Upvotes: 1
Reputation: 1684
From Qt Assistance
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time.
So passing index is a key thing to identify the widget need to be displayed on StackedLayout at a particular point of time. Suppose your signal name is "activated(int)" declared in both mainWidget_ and createGameWidget_
So you need to connect like this
//MainWindowWidget class. connect(MainWidget, SIGNAL(activated(int)), widgets_ , SLOT(setCurrentIndex(int))); connect(createGameWidget_, SIGNAL(activated(int)), widgets_ , SLOT(setCurrentIndex(int)));
//In MainWidget class you need to emit signal
MainWidget::ChangeLayout()
{
emit activated(1); //createGameWidget_will be displayed
}
//In createGameWidget_class you need to emit signal
createGameWidget_::ChangeLayout()
{
emit activated(0); //MainWidget will be displayed
}
Upvotes: 0