开发者

Help on QStackedWidget

I am using a QStackedWidget on my mainWindow. The firstPageWidget on stackedWidget contains 3 buttons. Now If I wi开发者_JAVA技巧ll 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.


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)));


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜