2 Attachment(s)
Accessing a the click() slot of a button generated during runtime
I have a GUI project in Qt Creator that functions as a shopping list. I am using a QLineEdit to add items to a QTableWidget. The user types something in, presses the QPushButton. The slot then adds a new row to the QTableWidget with the input, in the first column, and a new QPushButton in the second column. I then want the user to be able to press the button and have it clear that row, but I don't know how to access that slot, or sender (I'm not sure the proper term.) Here is the code so far. ui->itemList is my QTableWidget and ui->itemInput is the QLineEdit.
Code:
void MainWindow::on_btnAddItem_clicked()
{
ui->itemList->insertRow(ui->itemList->rowCount());
ui
->itemList
->setItem
((ui
->itemList
->rowCount
())-1,
0,
new QTableWidgetItem(ui
->itemInput
->text
()));
ui->itemList->setIndexWidget(ui->itemList->model()->index(ui->itemList->rowCount()-1, 1), clear);
ui->itemInput->clear();
}
Here is when the program is initially run. Once they click the button, it runs
Code:
on_btnAddItem_clicked()
Attachment 11802
Then after pressing the add item button, it looks like this:
Attachment 11803
I want to make the clear button remove the row it is a part of.
Do I need to create a new slot? Any help?
Re: Accessing a the click() slot of a button generated during runtime
You can use a single slot and use QObject::sender() to determine which button the click came from.
What you will additionally need is a reverse mapping from button to the index you positioned it on.
One option is to store the return value of model()->index() in a QModelIndex variable, use that with setIndexWidget() and convert it to a QPersistantModelIndex which you store in a dynamic propery of the button object.
Cheers,
_