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. itemList
is my QTableWidget, itemInput
is the QLineEdit.
void MainWindow::on_btnAddItem_clicked()
{
ui->itemList->insertRow(ui->itemList->rowCount());
ui->itemList->setItem((ui->itemList->rowCount())-1,0,new QTableWidgetItem(ui->itemInput->text()));
QPushButton *clear = new QPushButton("Clear",this);
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 on_btnAddItem_clicked()
Then it looks like this, and 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?
clicked()
signal to a slot in MainWindow. Also don't use theon_
naming convention for the new slot you create since this will not be an automatic connection. – drescherjmclicked()
signal to a certain slot of theQTableWidget
(or its model or delegate?) with a correspondingQTableWidgetItem
as input parameter so that it is possible to edit/erase the content of thatQTableWidgetItem
. The signalclicked()
is only able to passbool
as parameter, so how would they access which row to erase. Does it sound right ? – vicrucannclicked()
ofQPushButton
that you create dynamically which is called "Clear" on your screenshot. Does it make sense now? – vicrucann