Olumide
Olumide

Reputation: 5839

Connecting QWidget to slot QStackedWidget::setCurrentWidget

I'm attempting to toggle stacked push buttons as show in the following application.

Declaration:

#include <QPushButton>
#include <QMainWindow>
#include <QStackedWidget>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

private:
   QPushButton* m_button[2];
   QStackedWidget *m_buttonStack;
};

Implementation (take note of the connects):

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    resize(300, 300);

    m_buttonStack = new QStackedWidget( this );
    m_buttonStack->setGeometry( 100, 100 , 100 , 100 );

    m_button[0] = new QPushButton( this );
    m_button[0]->setText( "Button 1" );
    m_buttonStack->addWidget( m_button[0] );

    m_button[1] = new QPushButton( this );
    m_button[1]->setText( "Button 2" );
    m_buttonStack->addWidget( m_button[1] );

    m_buttonStack->setCurrentWidget( m_button[1] );

    QObject::connect( m_button[0] , SIGNAL( clicked() ) , m_buttonStack , SLOT( setCurrentWidget( m_button[1] ) ) );
    QObject::connect( m_button[1] , SIGNAL( clicked() ) , m_buttonStack , SLOT( setCurrentWidget( m_button[0] ) ) );
}

For some inexplicable reason, although QStackedWidget::setCurrentWidget is defined QObject::connect is unable to connect to it, as the following error messages, copied from the application output window, show:

Object::connect: No such slot QStackedWidget::setCurrentWidget( m_button[1] ) in ..\mainwindow.cpp:21
Object::connect: No such slot QStackedWidget::setCurrentWidget( m_button[0] ) in ..\mainwindow.cpp:22

Upvotes: 0

Views: 1807

Answers (2)

ratchet freak
ratchet freak

Reputation: 48216

you'll want to use a QSignalMapper

connect(m_button[0], SIGNAL(clicked()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(m_button[0],m_button[1]);
connect(m_button[1], SIGNAL(clicked()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(m_button[1],m_button[0]);

QObject::connect(signalMapper , SIGNAL( mapped(QWidget *) ) , m_buttonStack , SLOT( setCurrentWidget(QWidget *) ) );

Upvotes: 2

vahancho
vahancho

Reputation: 21248

You don't need to provide the actual argument(s) when refer to the function signature in the SLOT macro:

QObject::connect( m_button[0], SIGNAL( clicked() ), m_buttonStack, SLOT( setCurrentWidget( m_button[1] ) ) );

Should be something like this:

QObject::connect( m_button[0], SIGNAL( clicked() ), m_buttonStack, SLOT( setCurrentWidget( QWidget *) ) );

As you could see, even in this way, the connection will not work. I suggest to implement your own slot (without arguments) and connect your buttons clicks to it:

QObject::connect(m_button[0], SIGNAL(clicked()), this, SLOT(onButtonClicked()));

than, in the slot, you can handle the click:

void MainWindow::onButtonClicked()
{
    // Get the button clicked
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    m_buttonStack->setCurrentWidget(btn);
}

Thus you can even remove your buttons array.

Upvotes: 2

Related Questions