Results 1 to 4 of 4

Thread: Signal parameter - needed?

  1. #1
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Signal parameter - needed?

    I have the signals from a pushButton and a checkBox both connected to the same slot:

    connect(pushButton_redraw_dist_plots, SIGNAL(clicked()), this, SLOT(drawDistPlots()));
    connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(stateChanged()), this, SLOT(drawDistPlots()));

    The pushButton signal does invoke drawDistPlots(), but the checkBox signal does not. I know that clicked() takes a bool argument, and stateChanged() takes an int argument, but I'm not sure if the arguments are needed. From what I observe, it seems that the clicked() signal functions without the argument, but apparently the stateChanged() signal does not. This seems odd. I am omitting the arguments because I don't know how to make drawDistPlots() take either a bool or an int argument. Is there a way to do what I want?

    Thanks
    Gib

    Edit:
    I decided to try making drawDistPlots() take a dummy int argument, then use these connect statements:

    connect(pushButton_redraw_dist_plots, SIGNAL(clicked(bool)), this, SLOT(drawDistPlots(int)));
    connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(stateChanged(int)), this, SLOT(drawDistPlots(int)));

    This compiles (to my surprise), but the pushButton signal is not received.
    Last edited by gib; 18th October 2018 at 02:34.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal parameter - needed?

    Hi, you need to add the parameters in the signal when you call connect, but you can omit them in the slot, so you don't need a dummy parameter.
    I'm not sure about connecting a signal with a bool parameter to a slot with an int, but I think it should print an error at runtime.
    This should work:
    Qt Code:
    1. connect(pushButton_redraw_dist_plots, SIGNAL(clicked()), this, SLOT(drawDistPlots()));
    2. connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(stateChanged(int)), this, SLOT(drawDistPlots()));
    To copy to clipboard, switch view to plain text mode 

    Ginsengelf

    edit: you could also use the new Qt5 syntax: https://wiki.qt.io/New_Signal_Slot_Syntax

  3. #3
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Signal parameter - needed?

    Thanks. After some testing, I found that this works:

    connect(pushButton_redraw_dist_plots, SIGNAL(clicked(bool)), this, SLOT(drawDistPlots(bool)));
    connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(toggled(bool)), this, SLOT(drawDistPlots(bool)));

    From what you say I could omit the dummy parameter in the slot.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signal parameter - needed?

    A little late, but I was on vacation and couldn't log into the forum and the "forgot password" link still doesn't work.

    connect(pushButton_redraw_dist_plots, SIGNAL(clicked(bool)), this, SLOT(drawDistPlots(bool)));
    If your slot does not use the bool argument, then there is no need to define the slot method with a bool argument. That is, your slot can simply be:

    Qt Code:
    1. void MyClass::drawDistPlots()
    To copy to clipboard, switch view to plain text mode 

    The Qt signal / slot system allows slots to use fewer arguments than the signals they are connected to. For example, if the signal has two parameters, then the slot can use both of them, only the first one, or none at all.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Slot - Signal with parameter
    By Lodhart in forum Newbie
    Replies: 8
    Last Post: 10th April 2013, 10:08
  2. A Signal with Parameter from Qt C++ to QML
    By reinki0013 in forum Qt Quick
    Replies: 1
    Last Post: 8th November 2011, 13:12
  3. Replies: 3
    Last Post: 26th August 2011, 07:17
  4. Struggling with signal which has a default parameter.
    By JPNaude in forum Qt Programming
    Replies: 21
    Last Post: 21st January 2011, 08:40
  5. Replies: 2
    Last Post: 6th July 2009, 12:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.