Results 1 to 7 of 7

Thread: Passing an integer to a slot

  1. #1
    Join Date
    Jul 2009
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Passing an integer to a slot

    Hello, all;

    Does anyone know the best way to pass an integer to a slot? I'm currently having trouble doing so and am receiving a “no such slot” message when I run my program.
    It compiles and runs fine if I don't try to pass anything to it (i.e. declare it “void”), and even compiles when I do; it's only upon execution that I get the error message. Here are the relevant code snippets I'm using for the signal connection and slot in the .cpp file:

    Qt Code:
    1. int currentFreq1 = frequency_ch1_SB->value();
    2.  
    3. qDebug() << currentFreq1 << " Frequency value to pass to Start IO #1\n";
    4.  
    5. connect( taStartPB, SIGNAL( clicked() ), this, SLOT( startIO(currentFreq1) ));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ConfigurationPage::startIO(int currentFreq1)
    2. {
    3. ...
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    ...and in the .h file, the slot is defined this way:

    Qt Code:
    1. class ConfigurationPage : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ConfigurationPage(QWidget *parent = 0);
    7.  
    8. private slots:
    9. void startIO(int);
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    The specific error message I'm getting is: “No such slot ConfigurationPage::startIO(currentFreq1) in toneaud_pages.cpp:348
    ”. If anyone has any suggestions, I'd certainly appreciate it.

    Thanks,

    Allan
    Last edited by bizmopeen; 27th October 2009 at 23:03.

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing an integer to a slot

    Try this:
    Qt Code:
    1. connect( taStartPB, SIGNAL( clicked() ), this, SLOT( startIO(int) ));
    To copy to clipboard, switch view to plain text mode 
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. #3
    Join Date
    Jul 2009
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Passing an integer to a slot

    Quote Originally Posted by calhal View Post
    Try this:
    Qt Code:
    1. connect( taStartPB, SIGNAL( clicked() ), this, SLOT( startIO(int) ));
    To copy to clipboard, switch view to plain text mode 
    Yeah, I thought of that as well, but then I get a "QObject::connect: Incompatible sender/receiver arguments
    QPushButton::clicked() --> ConfigurationPage::startIO(int)
    " message. Plus, if I don't tell the signal which integer to pass, how will the slot know which value to work with?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing an integer to a slot

    The signal must emit the integer which the slot will use, you can't specify the value in the connect call, you only specify the function signature there, and this signature must be the same as that specified in your header file.

    Qt Code:
    1. connect( taStartPB, SIGNAL( clicked(int) ), this, SLOT( startIO(int) ));
    To copy to clipboard, switch view to plain text mode 

    You'll also notice that SIGNAL and SLOT are just macros which turn there argument into a string, therefore passing a variable or value is useless. If you look into the generated moc_*.cpp files you'll see these signatures inside the qt_meta_stringdata array.
    Last edited by squidge; 27th October 2009 at 23:09.

  5. #5
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing an integer to a slot

    Ups, I forgot about something
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  6. #6
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Passing an integer to a slot

    You can use QSignalMapper between your pushbutton and the receiver to add that integer to your data flow.
    It's nice to be important but it's more important to be nice.

  7. #7
    Join Date
    Aug 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing an integer to a slot

    Instead of using a QSignalMapper, which I think don't suit you(the int is not always the same, you will need to call setMapping each time...), I suggest you simply create an additional slot (void) which will call startIO(currentFreq1). (and simply put startIO private)

    So connect the QPushButton::clicked() to the additional slot:
    Qt Code:
    1. QObject::connect(taStartPB, SIGNAL( clicked() ), this, SLOT(additionalSlot());
    To copy to clipboard, switch view to plain text mode 
    Then you can call startIO(int) :
    Qt Code:
    1. void ConfigurationPage::additionalSlot()
    2. {
    3. int currentFreq1 = frequency_ch1_SB->value();
    4. startIO(currentFreq1)
    5. }
    To copy to clipboard, switch view to plain text mode 

    I think it 's a straightforward way, isn't it?

    Hope to be useful...

Similar Threads

  1. Passing values to custom 'slot' functions (pyqt)
    By Richie in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 08:05
  2. Replies: 12
    Last Post: 18th September 2008, 16:04
  3. passing arguments in SLOT
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2008, 22:55
  4. Problem When Creating my own Slot
    By Fatla in forum Qt Programming
    Replies: 12
    Last Post: 6th June 2008, 15:44

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.