check for clicks on buttons inside a vector?
-
I've got the following function to create a button when another button is pressed
void NoteTest::on_btn_add_note_clicked() { QPushButton *crt = new QPushButton(ui->frame_note_list); crt->setText(QString("text")); crt->move(0, 30*total_notes); crt->resize(186, 30); crt->setStyleSheet(QString("QPushButton {color: #444444; background: #e6e6e6; border: 2px solid #cccccc; border-width: 0px 0px 2px 0px;} QPushButton:hover {border: 2px black solid; color: #333333;background: #F7F7F7;} QPushButton:pressed {background: #e6e6e6;}")); crt->show(); v_notes.push_back(crt); total_notes += 1; }
Now, how do check when one of those buttons are pressed? And can I know which one of the buttons that was pressed?
-
I changed the code to
void NoteTest::on_btn_add_note_clicked() { QPushButton *crt = new QPushButton(ui->frame_note_list); connect(crt, SIGNAL(clicked()), SLOT(on_btn_note_clicked())); crt->setText(QString("text")); crt->move(0, 30*total_notes); crt->resize(186, 30); crt->setStyleSheet(QString("QPushButton {color: #444444; background: #e6e6e6; border: 2px solid #cccccc; border-width: 0px 0px 2px 0px;} QPushButton:hover {border: 2px black solid; color: #333333;background: #F7F7F7;} QPushButton:pressed {background: #e6e6e6;}")); crt->show(); v_notes.push_back(crt); total_notes += 1; } void NoteTest::on_btn_note_clicked() { qDebug() << "clicked!"; }
and now it says "clicked!" when I press a button, but I do not know WHICH one that was pressed...
-
Hi
Why do you need to know which buttons?
Anyway, you can use sender()void NoteTest::on_btn_note_clicked() { QPushButton * theButton = qobject_cast<QPushButton *> ( sender() ); if(theButton) { //do what you need } qDebug() << "clicked!"; }
-
I need to know what button has been pressed (not the full history) in order to do the right action. Your way, using sender, worked perfectly! Thanks!
-
What are the "right actions" ?
-
The "right actions" is open the right document when I press a certain button in this vector.
-
Rather than using a bunch of ifs, what about using a QMap or a QHash to store the information and retrieve that in the slot ? This will make your implementation easier to maintain.