Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView: Add a button to each row of the table to get the row number.
Forum Updated to NodeBB v4.3 + New Features

QTableView: Add a button to each row of the table to get the row number.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LimeLight
    wrote on last edited by
    #1

    I want to add a button at the end of each row which on pressed will display a message "The Row No is <no.>".
    I have added a button but I can't figure out how can I retrieve the row number.
    Here's my code about how I added the buttons

    for(int i = 0;i<model->rowCount();i++)
            {
                QPushButton *cartButton = new QPushButton();
                QPixmap pixmap(":/test/info.png");
                QIcon buttonIcon(pixmap);
                ui->tableView->setIndexWidget(model->index(i,2),cartButton);
                cartButton->setIcon(buttonIcon);
            }
    

    Here's how the table is form.
    0efae788-499a-454a-a3cc-b279f79a52d7-image.png

    I just want to know what changes I must do to my code so that whenever I press the button, I get a popup message displaying the row number.

    I use Qt creator 6.3.1 on windows 10.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      One way is to use a lambda as slot and capture a copy of I for the connect

      for(int i = 0;i<model->rowCount();i++)
      {
      ......
      ......
       connect(cartButton , &QPushButton::clicked, [ i ]()
       {
           qDebug()<<"clicked" << i ;
       });
      
      .....
      
      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved