You are not logged in.

1

Saturday, March 28th 2009, 1:15am

Switch stacked widget from button

Hello.

Is it possible to switch a page in a stacked widget with a button? If, how do I do it?

2

Saturday, March 28th 2009, 2:20am

I am assuming you're using Qt Designer.

1/ In the Form Editor, place a stacked widget and a button. Right-click the button and select Go to slot... Then choose clicked(). This automatically generates the signal handler and opens the Source Editor.
2/ Type :

Source code

1
ui->stackedWidget->setCurrentIndex(1);

where 1 is the page index (counted from 0).
Mandriva Linux release 2009.0 (Official) for i586
Kernel 2.6.27.10-desktop-1mnb on a Dual-processor i686

3

Saturday, March 28th 2009, 8:12am

Can't find anything when right-clicking that says Go to slot. Can you give me an example when using connect and SIGNAL/SLOT

4

Saturday, March 28th 2009, 8:40pm

Sorry, in my previous message, read « Qt Creator » instead of « Qt Designer ». The new development environment Qt Creator allows you to automatically generate signal handlers.

But, if you want to do it manually, put in your header file :

Source code

1
2
private slots:
    void OnButtonClick();

Put in your source file :

Source code

1
2
3
4
5
6
7
8
9
10
11
12
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
...
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(OnButtonClick()));
}

void MainWindow::OnButtonClick()
{
    ui->stackedWidget->setCurrentIndex(1);
    qDebug("index changed"); // for testing purposes
}
Mandriva Linux release 2009.0 (Official) for i586
Kernel 2.6.27.10-desktop-1mnb on a Dual-processor i686