PDA

View Full Version : Connect lambda, sender argument must be string from array



bchinfosieeuw
24th December 2016, 21:34
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 ...'



btnTest = new QPushButton[pagecounter];//("G", this);
btnTest[pagecounter-1].setMaximumSize(QSize(20,20));

btnTest[pagecounter-1].setObjectName(QString::number(pagecounter-1));

top->addWidget(groupBox);
top->addWidget(btnTest, pagecounter-1);
top->maximumSize();

mainLayout->addLayout(top);

QVBoxLayout *vbox = new QVBoxLayout;
groupBox->setLayout(vbox);

connect(btnTest[pagecounter-1], &QPushButton::clicked, [=] {
emit systembutton_clicked(btnTest[pagecounter-1].objectName().toInt(), 1, vbox);
});

NameRakes
24th December 2016, 22:27
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!!

d_stranz
24th December 2016, 22:33
Your lambda syntax is wrong. It needs to be a functor:



connect(btnTest[pagecounter-1], &QPushButton::clicked, [=] () {
emit systembutton_clicked(btnTest[pagecounter-1]->objectName().toInt(), 1, vbox);


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.

NameRakes
24th December 2016, 23:15
Good point! Missed the pointer aspect with my quick perusal.

anda_skoa
25th December 2016, 11:42
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,
_

bchinfosieeuw
25th December 2016, 21:22
I would like to initialize all the arrays in the for loop. Can you tell me how to do that?

d_stranz
25th December 2016, 21:40
Maybe something like:



for ( int i = 0; i < pagecounter; i++ )
{
// initialize btnTest[i]
}


You should have learned that in the first couple of weeks of your C++ class...

bchinfosieeuw
25th December 2016, 22:14
What I really want to do is disable the btnTest buttons when they are clicked a couple of times. But after invoking


void MyWidget::onaddbutton_clicked()
{

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:


void MyWidget::systembutton_clicked(int j, int k, QVBoxLayout *vbox)
{
...
if(...){
btnTest[j].setEnabled(false);

bchinfosieeuw
26th December 2016, 01:27
According to the btnTest2[i].isEnabled() function the button is disabled, but in the program the btnTest2[i] remains clickable.

anda_skoa
26th December 2016, 11:23
You onaddbutton_clicked() creates a new array of buttons every time, maybe you disabled only one of these sets.

Cheers,
_