tags:

views:

123

answers:

3

Hi Geeks,

I am using a QStackedWidget on my mainWindow. The firstPageWidget on stackedWidget contains 3 buttons. Now If I will press the first button on firstPage, I want the stackedWidget show the 2nd Page widget. Following are the details

I tried to connect like this in my mainwindow

connect(firstPageWidget->button1,SIGNAL(clicked()),stackWidget,SLOT(setCurrentIndex(int)));

Now I want to know how to pass the value of index number to stackWidget to set currentIndex?

If my question is not much clear please tell me I will explain more.

+3  A: 

You probably need to use QSignalMapper class:

QSignalMapper mapper = new QSignalMapper(); // don't forget to set the proper parent
mapper->setMapping(firstPageWidget->button1, 2); // 2 can be replaced with any int value
connect(firstPageWidget->button1, SIGNAL(clicked()), mapper, SLOT(map()));
connect(mapper, SIGNAL(mapped(int)), stackWidget, SLOT(setCurrentIndex(int)));
chalup
A: 

Instead of buttons you could use a QListWidget containing selectable Icons. QListWidget has a method called QListWidget::currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous ). I used that method in a program I wrote some time ago... There I wrote a function on_change_widget( QListWidgetItem *current, QListWidgetItem *previous ) which I connected to the SIGNAL currentItemChanged:

connect( my_list_widget,
         SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
         this,
         SLOT(on_change_widget(QListWidgetItem *, QListWidgetItem *))
       );

That should do the trick.

You should have a look at the Config Dialog Example, there they use the same method to swap widgets.

Of course you can use normal buttons too, in connection with the QSignalMapper chalup mentioned.

Exa
A: 

You can also use QButtonGroup and set proper id to each button, then connect QButtonGroup signal QButtonGroup::buttonClicked(int) with stacked widget slot QStackedWidget::setCurrentIndex(int)

Kamil Klimek