buddy
buddy

Reputation: 3

How can I add values of different variables to a list upon click of button?

Right now I am trying to create a shopping list program in which the user will add items to shopping list by clicking on a button. Please take a look at the screenshot, it will help.

So What I am trying to do is adding each ingredient to the text browser when clicking on the Tool Button next to it.

I tried using ui->listView->setText(), but I couldn't figure out how to pass a variable with it. Also, when I click on the button the list will only show one item at a time. I need to show all the items at the same time.

This my current program

 void shoppingList::on_add1_clicked()
{
    ui->listView->setText(ui->label_2); // this doesn't work
}

void shoppingList::on_add2_clicked()
{
    ui->listView->setText("ing2"); // this works but no efficient because only 1 item will be in list
                                      // + I need to pass a variable 
}

Upvotes: 0

Views: 145

Answers (2)

Maxim Paperno
Maxim Paperno

Reputation: 4869

void shoppingList::on_add1_clicked()
{
  ui->listView->append(ui->item_1->text());  // where ui->item_1 is a QLineEdit
  ui->listView->ensureCursorVisible();  // will scroll to bottom of list if necessary
}

Reference: QTextEdit::append(), QTextEdit::ensureCursorVisible()

Upvotes: 1

Hayfa
Hayfa

Reputation: 147

If I understand your problem , I think you should use void QTextEdit::append ( const QString & text ) to append new ingredients to the listview.

And you should use ui->listView->setText(ui->label_2->text()); to get the text displayed on the label.

(Although I am not seeing any label_2 object in the screenshot you attached to the post. So to add the ingredients , you probably should use ui->listView->append(ui->item_2->text()); )

Upvotes: 0

Related Questions