PDA

View Full Version : stack widget pages using pushbutton



arunvv
20th March 2008, 18:44
Hi

How to navigate stackwidget pages using PushButtons?
Which method needs to be called whenever pushbutton is clicked, so that it opens a particluar stackwidget page.

Thanks & Regards,
Arun.

marcel
20th March 2008, 19:01
You must use the setCurrentIndex(), currentIndex() and count() methods of QStackedWidget.

arunvv
20th March 2008, 21:25
Can you help in navigating 3 buttons with the help a small piece of code.

Thanks & Regards,
Arun

arunvv
20th March 2008, 22:05
Thanks, it solved now.
For those who implement stack widget pages using pushbutton, here is snippet of code.
in a simple way. As I am using 3-4 buttons it is easy to use this way.
May be for more number of buttons you can choose any other method for best use.

Code:

static int currentIndex = 0; // Global
.......
......
....
class::class() ///constructor
{
QObject::connect(pushbutton1, clicked(), this, page1());
QObject::connect(pushbutton2, clicked(), this, page2());
}
void class::page1()
{
currentIndex = stackedWidget->currentIndex();
if( currentIndex < stackedWidget->count())
{
stackedWidget->setCurrentIndex(0); // page1
}
}
void class::page2()
{
currentIndex = stackedWidget->currentIndex();
if( currentIndex < stackedWidget->count())
{
stackedWidget->setCurrentIndex(1); // for page2
}
}

Note: For other buttons and pages replace stackedWidget->setCurrentIndex(number with appropriate page).

Thanks & Regards,
Arun.

jacek
21st March 2008, 11:09
Please don't use global variables --- you can do the same using member variables and your class will be reusable.

Also you can save a lot of typing by using QSignalMapper.