i cannot check my QPushbutton that is in my qtableview
-
here's the code of my connect :
@jsulm , this doesnt seem to work : Qt says that clicked() is an undeclared identifier when i take off SIGNAL/SLOT
my code now runs but only with the last button of the row.i suppose it's because the connect keeps assigning to the next button as the loop goes forward. is there a way to assign the connect() to each button on the loop ?
-
@aftalib said in i cannot check my QPushbutton that is in my qtableview:
Qt says that clicked() is an undeclared identifier when i take off SIGNAL/SLOT
You have to read and act on https://wiki.qt.io/New_Signal_Slot_Syntax.
here's the code of my connect
You show no
connect()
statement. You show your proposed slot code instead. -
@aftalib
Earlier I only see:index = m_table_space_pos->index(1,1,QModelIndex());
I do not see where you are creating
QPushButton
s in a loop, putting each one into the table, and connecting each one to the slot. I see you only addressing(1, 1)
in the table.Further, the slot only looks at one
H11
variable. I do not know how thatH11
relates to each of the push buttons you created.If you seem to intend
AntArrayDialog::on_H11_clicked()
slot to handle any of the buttons pressed (connect()
ed to each one), it is going to need to know which button was clicked. And that will require you to use a C++ lambda for the connection, to pass which button was clicked as a parameter to the slot....Start by just verifying your slot gets called for any button clicked, not just one button....
-
@JonB my bad i hadnt updated my code at the time because i was doing tests to see if it was working for one button but i replaced (1,1) with (1,i) so i indeed have a row of buttons.
my slot gets called for the last button that is in the row of my TableViewi'm going to see about the C++ lambda you're talking about, it might be the solution i was looking for
-
@aftalib
You will need the lambda when you are ready to have the single slot code method handle the clicking of any of the push buttons, which I suspect is what you will want to achieve.However, before you do that, put a
qDebug() << "clicked"
in as the first statement in the slot. Verify that gets called when any of the buttons you created are clicked, not just one button. Only at that point are you ready to move to a lambda for the slot which will pass which button was pressed as a parameter to the slot.... -
@aftalib
The very first section there shows an example of changing fromSIGNAL
/SLOT()
macrosconnect( sender, SIGNAL( valueChanged( QString, QString ) ), receiver, SLOT( updateValue( QString ) ) );
over to
connect( sender, &Sender::valueChanged, receiver, &Receiver::updateValue );
The code you have pasted does not attempt to follow that required pattern.
-
okay thats my bad i didnt quite understand the syntax that was written in the new connect statement but i think i got it now, thank you. here's what i wrote :
connect(H11, &QPushButton::clicked,this,&AntArrayDialog::on_H11_clicked);
i dont get any errors and the program launches well
-
@aftalib
Yes, this is good :)Now, I believe, since (I assume) you will have each of the buttons connect to this one slot method, you will want the slot to receive a parameter to tell it which particular button was clicked this time to call the slot, right?
To do that you are going to need to move on to
connect()
ing a C++ lambda instead of the slot function as you currently do now. Then a parameter can be added, so that the signal will pass it to the slot. the parameter will identify which button emitted the signal.That new-style syntax page includes the following example:
Can be used with C++11 lambda expressions:
connect( sender, &Sender::valueChanged, [=]( const QString &newValue ) { receiver->updateValue( "senderValue", newValue ); } );
The lambda bit is:
[...](...) { ... }
See if you can figure how to pass the
QPushButton *
object from the signalling push button to the slot.... -
@JonB okay, just before you answered i tried this :
connect(H11, &QPushButton::clicked,[this](){ if (H11->isChecked()) { H11->setText("H2"); qDebug() << "test"; } else if (H11->isChecked()==false){ H11->setText("H1"); } });
so if i click on any button i want it to change to H2, then to H1 if i reclick on it and so on..
i didnt get any errors but it didnt work so im assuming this is wrong. im gonna retry something else with what you said -
@aftalib
At least you have you lambda syntax correct, that is good!Your
connect()
is not attempting to pass which button was clicked, as a parameter to the slot. The slot only references whatever one buttonthis->H11
currently refers to when it is executed. If you are wanting the slot to know which button invoked it in order to do its work, you are going to need that as a parameter to the slot in the lambda.