0
votes

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()

initial run

Then it looks like this, and I want to make the clear button remove the row it is a part of.

table entry

Do I need to create a new slot? Any help?

1
Do I need to create a new slot? Yes if you don't have one. I see you are creating the new button but not connecting its clicked() signal to a slot in MainWindow. Also don't use the on_ naming convention for the new slot you create since this will not be an automatic connection.drescherjm
@drescherjm, from my understanding, he/she is trying to connect the clicked() signal to a certain slot of the QTableWidget (or its model or delegate?) with a corresponding QTableWidgetItem as input parameter so that it is possible to edit/erase the content of that QTableWidgetItem. The signal clicked() is only able to pass bool as parameter, so how would they access which row to erase. Does it sound right ?vicrucann
No no, the clicked() signal I already have adds the user input to the table along with a new PushButton in the same row. I then want the user to be able to push the button and have it clear the row. That way, they can delete entries from the table. But I cannot pre-create the rows and buttons because I don't know how many entries a user will enter, so I'm not sure how to make a button do something if it doesn't exist until the program is running.Kenta
I made edits to hopefully make it more clear.Kenta
@Kenta, I referred to the signal clicked() of QPushButton that you create dynamically which is called "Clear" on your screenshot. Does it make sense now?vicrucann

1 Answers

0
votes

You will need to make your own button class and inherit QPushButton. Something like this :

class MyButton : public QPushButton {
    public:
    MyButton();
    QTableWidgetItem *titem;
}

And here you MainWindow :

void MainWindow::on_btnAddItem_clicked()
{
    ui->itemList->insertRow(ui->itemList->rowCount());
    ui->itemList->setItem((ui->itemList->rowCount())-1,0,new QTableWidgetItem(ui->itemInput->text()));
    MyButton *clear = new MyButton("Clear",this);
    clear->titem = ui->itemList->item(ui->itemList->rowCount()-1, 0);
    connect(clear, SIGNAL(clicked()), SLOT(on_btnClear_Clicked()));
    ui->itemList->setIndexWidget(ui->itemList->model()->index(ui->itemList->rowCount()-1, 1), clear);
    ui->itemInput->clear();
}

void MainWindow::on_btnClear_Clicked()
{
    MyButton *btn = (MyButton*)QObject::sender();
    ui->itemList->removeRow(btn->titem->row());
}

Please note, it is only step to do it.