PDA

View Full Version : QStackedWidget



sattu
28th September 2011, 09:18
Hi Everyone,

To my mainwindow, I have added a StackedWidget. In the StackedWidget I have added 3 pages, say, they are:
1)page_Main
2)page_User
3)page_Logs

page_Main is the default page which contains 2 pushbuttons(pB_User and pB_Logs). So the design is such that if I click pB_User, currentWidget changes to page_User and same for pB_Logs.

Now on page_User, I have a pushbutton (pB_BackUser), which when clicked should set the currentWidget to the previous page (i.e page_Main). I can do this easily by the following code:

void MainWindow::on_pB_BackUser_clicked()
{
ui->stackedWidget->setCurrentWidget(ui->page_Main);
}

But is there any more general way by which I can achieve this? :confused:
I mean some Qt Methods, which when called automatically take you to the previous page or widget? I tried with the following methods like :-

focusNextChild() , focusNextPrevChild() ,focusPreviousChild ()
but without much success. :(

I would be really glad if anyone can help me out, please.

With regards,
Sattu

nish
28th September 2011, 09:39
no. focus methods are only for widgets which are currently visible. You are doing it in the right way.

sattu
28th September 2011, 10:09
no. focus methods are only for widgets which are currently visible. You are doing it in the right way.

Thanks Nish, but what about this 2 methods:


previousInFocusChain (), nextInFocusChain ()

This 2 methods return Widget*. When I use them like this:

ui->stackedWidget->setCurrentWidget(ui->stackedWidget->previousInFocusChain());

But I get the following Application Output:

QStackedWidget::setCurrentWidget: widget 0x3eb2d8 not contained in stack :confused:

My main purpose is that I need a more general way of achieving this type of operations, instead of hard-coating the WidgetNames. Please help.

nish
28th September 2011, 13:39
Thanks Nish, but what about this 2 methods:


previousInFocusChain (), nextInFocusChain ()

dont you see "focus" in these methods also? they all fall in the same criteria i mentioned you about.



This 2 methods return Widget*. When I use them like this:

ui->stackedWidget->setCurrentWidget(ui->stackedWidget->previousInFocusChain());

But I get the following Application Output:
:confused:

My main purpose is that I need a more general way of achieving this type of operations, instead of hard-coating the WidgetNames. Please help.

use index instead of widgets. something like-


void setPreviousWidget()
{
int index = stackWidget->currentIndex() - 1;

if ( index < 0 )
index = stackWidget->count() - 1;//go to last widget (or do not change if you like, return from here).

statckWidget->setCurrentIndex(index);

}

sattu
28th September 2011, 14:03
Thanks a lot Nish,

This INDEX stuff works a lot great. Thanks once again for the help. :o

With Regards,
Sattu.

nish
28th September 2011, 14:32
Thanks a lot Chris,


You got my nick wrong. :)

sattu
28th September 2011, 14:44
Oops sorry, My mistake. :rolleyes: Thanks Giving Statement modified :)