Results 1 to 2 of 2

Thread: Accessing a the click() slot of a button generated during runtime

  1. #1
    Join Date
    Mar 2016
    Posts
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default 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.
    Qt Code:
    1. void MainWindow::on_btnAddItem_clicked()
    2. {
    3. ui->itemList->insertRow(ui->itemList->rowCount());
    4. ui->itemList->setItem((ui->itemList->rowCount())-1,0,new QTableWidgetItem(ui->itemInput->text()));
    5. QPushButton *clear = new QPushButton("Clear",this);
    6. ui->itemList->setIndexWidget(ui->itemList->model()->index(ui->itemList->rowCount()-1, 1), clear);
    7. ui->itemInput->clear();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Here is when the program is initially run. Once they click the button, it runs
    Qt Code:
    1. on_btnAddItem_clicked()
    To copy to clipboard, switch view to plain text mode 

    shopping list prog.PNG

    Then after pressing the add item button, it looks like this:

    shop prog 2.PNG

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. accessing buttons from slot
    By dlthompson81 in forum Newbie
    Replies: 5
    Last Post: 15th April 2012, 22:15
  2. Replies: 1
    Last Post: 16th December 2011, 06:50
  3. Replies: 3
    Last Post: 10th August 2011, 18:52
  4. Replies: 11
    Last Post: 20th April 2011, 22:53
  5. Accessing generated pixmaps in html / Custom tooltips
    By Pieter from Belgium in forum Qt Programming
    Replies: 9
    Last Post: 1st August 2009, 16:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.