c++qtsignalsslot

cannot pass arguments in a signal/slot/connect routine


I want to do something pretty simple. Click on a toggle-button and get a message-box in QT.

cpp:

_show_hide_password = new QPushButton( "abc" );
_show_hide_password->setCheckable( true );
...
QMessageBox* show_password_warning = new QMessageBox ( this );
...
connect( _show_hide_password, SIGNAL ( toggled( bool ) ), this, SLOT ( showHidePw_ButtonAction( QMessageBox* show_password_warning ) ) );


void showHidePw_ButtonAction( QMessageBox* dialog ){...}

h:

public slots:
  void showHidePw_ButtonAction( QMessageBox* dialog);

No matter how I format the SLOT in the connect or in the .h, I cannot parse an argument to the slot. This is pretty annoying. However, it works when I do not pass arguments to the slot at all:

cpp:

connect( _show_hide_password, SIGNAL ( toggled( bool ) ), this, SLOT ( showHidePw_ButtonAction() ) );

h:

public slots:
  void showHidePw_ButtonAction( );

But I am not allowed to keep the QMessageBox* show_password_warning global in the .h-data.

How to get pass an argument the function declared in the signal?

I suppose that this is a pretty simple use-case. There should be a nice and clean solution which does not require some over-the-top blew up the code signal transforming functions.

It compiles without problem, but I get the following error after starting the program:

QObject::connect: No such slot PinEntryDialog::showHidePw_ButtonAction( QMessageBox* show_password_warning ) in pinentrydialog.cpp:198

Solution

  • The only parameters a slot should have are the same parameters as the signal.

    You have two options, both of which are fairly straightforward:

    1. Make show_password_warning a member variable of your class
    2. Create show_password_warning inside the showHidePw_ButtonAction() method and delete it when you're done with it.