PDA

View Full Version : how to change the index of QstackedWidget from a contained widget



sliverTwist
23rd March 2013, 16:19
I have a QStackedWidget that contains 2 layers of widgets(widget inside another widget). The final widget contains a multiplicity of buttons. How can i change the index of that QstackedWidget when i press one of those buttons ?? :confused: :confused: i should mention that the contained widget in the QstackedWidget is a separated widget so it does have it's own .cpp .h and .ui files

anda_skoa
24th March 2013, 13:38
setCurrentIndex() is a slot, so make the contained widget emit a signal that carries the desired index and connect it to the slot of the stacked widget instance.

Simply ways to map multiple buttons to different integer values are QButtonGroup and QSignalMapper

Cheers,
_

sliverTwist
24th March 2013, 14:30
setCurrentIndex() is a slot, so make the contained widget emit a signal that carries the desired index and connect it to the slot of the stacked widget instance.

Simply ways to map multiple buttons to different integer values are QButtonGroup and QSignalMapper

Cheers,
_

Well sir i didn't understand what you said ?? Did you understand my question ?? And could you give me an example of what you just mentioned ??

d_stranz
24th March 2013, 17:53
What he is saying is this:

- Your button has a signal, clicked().
- In the widget class that contains the button, create a slot and name it anything you want. I will name it onSwitchPageClicked().
- In the constructor for the widget class, connect the button's clicked() signal to the onSwitchPageClicked() slot.
- In this slot, call setCurrentIndex() for the stacked widget with the index of the page you want to change to.


Please note - it does not matter at all what the layout of code in your project's .cpp and .h files is, so it is unimportant to mention that as part of your post.

sliverTwist
25th March 2013, 08:56
Sorry still not clear enough

This is the code of my widget


#include "loginwidget.h"
#include "ui_loginwidget.h"

#include <QDesktopServices>
#include <QUrl>
LoginWidget::LoginWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::LoginWidget)
{
ui->setupUi(this);
}

LoginWidget::~LoginWidget()
{
delete ui;
}


void LoginWidget::on_NewpushButton_clicked()
{
QDesktopServices::openUrl(QUrl("http://www.google.com", QUrl::TolerantMode));
}

void LoginWidget::on_ConfigurepushButton_clicked()
{
// Action an button clicked
}


I should mention that to show this widget in the stacked Widget i have implemented inside the stacked widget a widget that I've promoted to LoginWidget
could you help me modifying this code i am still a beginner in Qt

anda_skoa
25th March 2013, 10:14
1) add a signal to LoginWidget in loginwidget.h, something like void changeStackedWidgetIndex(int index);
2) in the appropriate slot emit it with the desired index
3) in your stacked widget class, connect the new signal to the setCurrentIndex slot, something like this


connect( ui->loginWidget, SIGNAL(changeStackedWidgetIndex(int)), this, SLOT(setCurrentIndex(int)) );


Cheers,
_

sliverTwist
25th March 2013, 11:14
thank you my problem was solved