Results 1 to 13 of 13

Thread: connect() - connecting to slot and passing arguments

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default connect() - connecting to slot and passing arguments

    Hello!

    I've got function:

    Qt Code:
    1. int myFunc(QString *param)
    2. {
    3. [...]
    4. }
    To copy to clipboard, switch view to plain text mode 

    and I want to connect that function to two different buttons, which will pass two different QString arguments. How can I do that?

    Qt Code:
    1. connect(button1, SIGNAL(clicked()), this, ?myFunc()?);
    2. connect(button2, SIGNAL(clicked()), this, ?myFunc()?);
    To copy to clipboard, switch view to plain text mode 

    Lets say that button1 should pass to function "abc" and button2 "def".

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect() - connecting to slot and passing arguments

    If i understood right, the best answer is signal mapper
    LE: You can also sub-class and connect the clicked signal with some signal you define with default arguments.

  3. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect() - connecting to slot and passing arguments

    I've read about signal mapper and I think this is the answer. But I don't understand how should I do it. Some piece of code would be very useful.

    thanks in advance
    best regards
    Tomasz

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connect() - connecting to slot and passing arguments

    Isn't the example of the docs (detailed description of QSignalMapper) enough?
    Qt Code:
    1. ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. signalMapper = new QSignalMapper(this);
    5.  
    6. QGridLayout *gridLayout = new QGridLayout;
    7. for (int i = 0; i < texts.size(); ++i) {
    8. QPushButton *button = new QPushButton(texts[i]);
    9. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    10. signalMapper->setMapping(button, texts[i]);
    11. gridLayout->addWidget(button, i / 3, i % 3);
    12. }
    13.  
    14. connect(signalMapper, SIGNAL(mapped(const QString &)),
    15. this, SIGNAL(clicked(const QString &)));
    16.  
    17. setLayout(gridLayout);
    18. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lykurg for this useful post:

    Tomasz (7th September 2010)

  6. #5
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect() - connecting to slot and passing arguments

    I've read this, but to be more specyfic I don't know how to use it. My code will look like this:

    Qt Code:
    1. ButtonWidget::ButtonWidget(QString text, QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. signalMapper = new QSignalMapper(this);
    5.  
    6. QPushButton *button = new QPushButton(texts);
    7. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    8. signalMapper->setMapping(button, text);
    9.  
    10. connect(signalMapper, SIGNAL(mapped(const QString &)),
    11. this, SIGNAL(clicked(const QString &)));
    12. }
    To copy to clipboard, switch view to plain text mode 

    And then in my code I'm doing something like this:

    Qt Code:
    1. ButtonWidget *btn = new ButtonWidget("test");
    To copy to clipboard, switch view to plain text mode 

    and then:
    Qt Code:
    1. connect(btn, signal(clicked()), this, SLOT(myFunc(String *param)));
    To copy to clipboard, switch view to plain text mode 

    right? And this will pass arguments? Or am I wrong?

    thanks in advance
    best regards
    Tomasz

    thanks in advance
    best regards
    Tomasz

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connect() - connecting to slot and passing arguments

    Quote Originally Posted by Tomasz View Post
    right? And this will pass arguments? Or am I wrong?
    Try it and you will see.
    Qt Code:
    1. QPushButton *button = new QPushButton(texts);
    To copy to clipboard, switch view to plain text mode 
    there is a 's' too much.
    Qt Code:
    1. connect(btn, signal(clicked()), this, SLOT(myFunc(String *param)));
    To copy to clipboard, switch view to plain text mode 
    You don't need that since the mapper does the work for you. therefore you have
    Qt Code:
    1. connect(signalMapper, SIGNAL(mapped(const QString &)),
    2. this, SIGNAL(clicked(const QString &)));
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Lykurg for this useful post:

    Tomasz (7th September 2010)

  9. #7
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect() - connecting to slot and passing arguments

    Yes, You're right about 's'. I've done it like this:

    Qt Code:
    1. #include "nowyGuzik.h"
    2.  
    3. nowyGuzik::nowyGuzik(QString text, QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. signalMapper = new QSignalMapper(this);
    7.  
    8. QPushButton *button = new QPushButton(text);
    9. button->setGeometry(0,0,40,40);
    10. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    11. signalMapper->setMapping(button, text);
    12.  
    13. connect(signalMapper, SIGNAL(mapped(const QString &)), this, SIGNAL(clicked(const QString &)));
    14. }
    To copy to clipboard, switch view to plain text mode 

    and in my code:

    Qt Code:
    1. nowyGuzik *btn = new nowyGuzik("test");
    2. ui->verticalLayout->addWidget(btn);
    To copy to clipboard, switch view to plain text mode 

    And I can't see any button. And still don't know how to connect that signal with my function. Sorry for my stupid questions but I've never used this signal mapper thing and still don't understand everything.

  10. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connect() - connecting to slot and passing arguments

    Qt Code:
    1. button->setGeometry(0,0,40,40);
    To copy to clipboard, switch view to plain text mode 
    Use a layout. Maybe you don't see the button since it is misplaced.

    Qt Code:
    1. connect(signalMapper, SIGNAL(mapped(const QString &)), this, SIGNAL(clicked(const QString &)));
    To copy to clipboard, switch view to plain text mode 
    "clicked()" is your slot! You have to define it and use SLOT instead of SIGNAL.

  11. The following user says thank you to Lykurg for this useful post:

    Tomasz (7th September 2010)

  12. #9
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect() - connecting to slot and passing arguments

    Yes, You're again right! Problems with layout - button was opening in new window. Now I've got everything like this:

    Qt Code:
    1. button = new QPushButton;
    2. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    3. signalMapper->setMapping(button, "test");
    4. connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(myFun(QString)));
    To copy to clipboard, switch view to plain text mode 

    and a couple lines down I've got:

    Qt Code:
    1. void MainWindow::myFun(QString parameter)
    2. {
    3. [...]
    4. }
    To copy to clipboard, switch view to plain text mode 

    And console give's me:

    Qt Code:
    1. Object::connect: No such slot MainWindow::myFun(QString *parameter)
    2. Object::connect: (receiver name: 'MainWindow')
    To copy to clipboard, switch view to plain text mode 

    What I'm doing wrong?


    ----------------------------------------------OK, solved my stupid mistake

    Another question - why If I create 4 buttons, and I click one my function is triggered 4 times? Should I have separate QSignalMapper for all buttons?

    thanks in advance
    best regards
    Tomasz
    Last edited by Tomasz; 7th September 2010 at 12:36.

  13. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect() - connecting to slot and passing arguments

    Did you declare "void MainWindow::myFun(QString *parameter)" with "public slots:" access specifier, in your class header file?

  14. #11
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect() - connecting to slot and passing arguments

    Yes, It's public. I'm creating my buttons like this:

    Qt Code:
    1. guziki1 = new QPushButton;
    2. connect(guziki1, SIGNAL(clicked()), signalMapper, SLOT(map()));
    3. signalMapper->setMapping(guziki1, "test");
    4. connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(myFun(QString)));
    5.  
    6. guziki2 = new QPushButton;
    7. connect(guziki2, SIGNAL(clicked()), signalMapper, SLOT(map()));
    8. signalMapper->setMapping(guziki2, "test");
    9. connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(myFun(QString)));
    10. [...]
    To copy to clipboard, switch view to plain text mode 

    And I'm clicking on button and function is triggered a couple times (as many as I have buttons).

    thanks in advance
    best regards
    Tomasz

  15. #12
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect() - connecting to slot and passing arguments

    That's because all your mapped signals have the same string: "test"

  16. #13
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect() - connecting to slot and passing arguments

    I've changed string - different for each button:

    Qt Code:
    1. for (int j=0; j<4; j++)
    2. {
    3. button[j] = new QPushButton;
    4. connect(button[j], SIGNAL(clicked()), signalMapper, SLOT(map()));
    5. signalMapper->setMapping(button[j], QString::number(j));
    6. connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(myFun(QString)));
    7. }
    To copy to clipboard, switch view to plain text mode 

    And if I click on firs button function is triggered 4 times with '0', on second - four times with '1'. Why?

    ---------------------------------------------------OK, again solved

    Qt Code:
    1. connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(myFun(QString)));
    To copy to clipboard, switch view to plain text mode 

    should be once.

    thanks
    best regards
    Tomasz

Similar Threads

  1. Replies: 3
    Last Post: 23rd February 2010, 04:33
  2. Replies: 1
    Last Post: 3rd February 2010, 04:45
  3. Can we connect QTimer::SingleShot with a slot taking arguments?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2008, 18:02
  4. passing arguments in SLOT
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2008, 21:55
  5. Replies: 2
    Last Post: 24th March 2008, 16:59

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.