PDA

View Full Version : Help with QStackedWidget



onírico
11th November 2009, 19:00
Hi everyone!!
I'm having some problems with a QStackedWidget and I though maybe you can help me. The program is a QWidget which contains a QStackedWidget and two QPushButtons (one, to change page; and another, to quit). The problem is that the "Change" button doesn't work and I have no idea why...
Here's the relevant code...

MenuManager::MenuManager(QWidget *parent): QWidget(parent)
{
menus = new QStackedWidget;
menus->addWidget( m1 = new MenuOne );
menus->addWidget( m2 = new MenuTwo );
menus->addWidget( m3 = new MenuThree);
menus->addWidget( m4 = new MenuFour );
menus->addWidget( m5 = new MenuFive );

/*menus->setCurrentIndex(3);*/ // Works fine, which means Menus' code is allright

QPushButton *bChange = new QPushButton("Change");
QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(3)));

QPushButton *bQuit = new QPushButton("Quit");
QObject::connect(bQuit, SIGNAL(clicked()), this, SLOT(close()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(menus);
layout->addWidget(bChange);
layout->addWidget(bQuit);
setLayout(layout);
}

Any help will be apreciated. Thanks!

spirit
11th November 2009, 19:22
this connection is wrong


QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(3)));//3 -- wrong
.
read carefully about signal&slots (http://doc.trolltech.com/4.5/signalsandslots.html)

onírico
12th November 2009, 00:12
Thank for spending your time reading my code, spirit.
I´ve read about signals and spots but I still haven't found the point. Could you give more clues?? I supose I have to define a slot function to change the page. I´m not sure how.

spirit
12th November 2009, 07:23
you is not careful. :)
your connection is


QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(3)));

it's not correct, because passing real values in connection IS NOT allowed.
so, your connection must look like


QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(int)));

i.e. you should specify SIGNATURE of method.

onírico
12th November 2009, 13:40
Ok. But now, how do I link that button with the MenuThree?? Do I have to use QSignalMapper??
what is exactly the signature (of a signal or slot)???

Thank you again, spirit!! And sorry for being worse than a headache.

spirit
12th November 2009, 13:46
add a slot which changes pages:


....
QObject::connect(bChange, SIGNAL(clicked()), this, SLOT(changePage()));
....
void MyWidget::changePage()
{
int nextPage = menus->currentIndex() + 1;
if (nextPage >= menus->count())
nextPage = 0;
menus->setCurrentIndex(nextPage);
}
...

should be something like this.

onírico
12th November 2009, 17:34
That wasn´t exactly what I wanted. But it helped me a lot to solve my problem.

Thank so much, spirit!!!