Results 1 to 10 of 10

Thread: Connect lambda, sender argument must be string from array

  1. #1
    Join Date
    Aug 2016
    Posts
    36
    Qt products
    Qt4
    Platforms
    Windows

    Default Connect lambda, sender argument must be string from array

    How can I send a string from an array with connect using the lambda-notation? I want to send btnTest[pagecounter-1] as the first argument of the connect, but the compiler says 'no matching function for call to ...'

    Qt Code:
    1. btnTest = new QPushButton[pagecounter];//("G", this);
    2. btnTest[pagecounter-1].setMaximumSize(QSize(20,20));
    3.  
    4. btnTest[pagecounter-1].setObjectName(QString::number(pagecounter-1));
    5.  
    6. top->addWidget(groupBox);
    7. top->addWidget(btnTest, pagecounter-1);
    8. top->maximumSize();
    9.  
    10. mainLayout->addLayout(top);
    11.  
    12. QVBoxLayout *vbox = new QVBoxLayout;
    13. groupBox->setLayout(vbox);
    14.  
    15. connect(btnTest[pagecounter-1], &QPushButton::clicked, [=] {
    16. emit systembutton_clicked(btnTest[pagecounter-1].objectName().toInt(), 1, vbox);
    17. });
    To copy to clipboard, switch view to plain text mode 
    Last edited by bchinfosieeuw; 24th December 2016 at 21:52.

  2. #2
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    31
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Connect lambda, sender argument must be string from array

    Ok, second attempt at replying.

    Is your intent to emit another signal from your slot (that is lambda)?

    If so, I would create local variables for the ones you want to use (capture) in the body of the lambda. Warning: with [=] you are capturing by value!!

  3. #3
    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: Connect lambda, sender argument must be string from array

    Your lambda syntax is wrong. It needs to be a functor:

    Qt Code:
    1. connect(btnTest[pagecounter-1], &QPushButton::clicked, [=] () {
    2. emit systembutton_clicked(btnTest[pagecounter-1]->objectName().toInt(), 1, vbox);
    To copy to clipboard, switch view to plain text mode 

    Note the parentheses following the [=]. And btnTest[pagecounter-1] is a pointer to QPushButton so needs to be dereferenced with pointer syntax.

    Warning: with [=] you are capturing by value!!
    Should not matter in this case, since the arguments in the lambda are pointers.
    <=== 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.

  4. #4
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    31
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Connect lambda, sender argument must be string from array

    Good point! Missed the pointer aspect with my quick perusal.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect lambda, sender argument must be string from array

    Also, from the previous thread, you don't need the emit, systembutton_clicked() is a method.

    And, again also from the previous thread, are you sure you want to create an array every time the slot is called and only initialize the last entry?

    Cheers,
    _

  6. #6
    Join Date
    Aug 2016
    Posts
    36
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connect lambda, sender argument must be string from array

    I would like to initialize all the arrays in the for loop. Can you tell me how to do that?

  7. #7
    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: Connect lambda, sender argument must be string from array

    Maybe something like:

    Qt Code:
    1. for ( int i = 0; i < pagecounter; i++ )
    2. {
    3. // initialize btnTest[i]
    4. }
    To copy to clipboard, switch view to plain text mode 

    You should have learned that in the first couple of weeks of your C++ class...
    <=== 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.

  8. #8
    Join Date
    Aug 2016
    Posts
    36
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connect lambda, sender argument must be string from array

    What I really want to do is disable the btnTest buttons when they are clicked a couple of times. But after invoking
    Qt Code:
    1. void MyWidget::onaddbutton_clicked()
    2. {
    To copy to clipboard, switch view to plain text mode 
    a second time, and then clicking on the old btnTest button of the first time, they do not get disabled. Any ideas of how to do this?

    The disabling methods are placed here:
    Qt Code:
    1. void MyWidget::systembutton_clicked(int j, int k, QVBoxLayout *vbox)
    2. {
    3. ...
    4. if(...){
    5. btnTest[j].setEnabled(false);
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2016
    Posts
    36
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connect lambda, sender argument must be string from array

    According to the btnTest2[i].isEnabled() function the button is disabled, but in the program the btnTest2[i] remains clickable.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect lambda, sender argument must be string from array

    You onaddbutton_clicked() creates a new array of buttons every time, maybe you disabled only one of these sets.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 25th August 2016, 16:24
  2. Replies: 6
    Last Post: 12th February 2012, 04:52
  3. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 08:28
  4. Replies: 1
    Last Post: 12th January 2009, 18:05
  5. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46

Tags for this Thread

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.