22

I require a button/link within a table row of a QTableView. This is to open a dialog to allow that row to be edited more efficiently.

After hours of looking on the web I am yet to find a decent example.

I am aware that this is likely to be done using a QItemDelegate, but I am unsure how to have a functional widget within the row without forcing the item into edit mode first.

Any help would be greatly appreciated.

cweston
  • 11,297
  • 19
  • 82
  • 107

2 Answers2

25

You can use setIndexWidget for that, see the Qt documentation for more information.

As an example, to embed a push button in the first column of the second row (untested code):

tableView->setIndexWidget(tableView->model()->index(2, 1), new QPushButton);
m7913d
  • 10,244
  • 7
  • 28
  • 56
Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
  • 4
    I have read that on large datasets, setting a row level widget can incur a noticeable performance loss. – cweston Dec 30 '10 at 14:23
1

You could emulate the functionality of a link by underlining the clickable text, then capturing the cell click via the cellClicked(row, col) signal and check that col == editColumn. Then row would correspond to which item you are editing.

For example,

Data Name | Value 1 | Value 2 | Edit

connect (tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(editSlot(int, int)));

...

void ClassName::editSlot(int row, int col){
  if (col == 3) {
    doWork(row);
  }
}
GatorGuy
  • 306
  • 1
  • 3
  • 7
  • 5
    Why does the accepted answer invariably fail to actually answer the question? Defining a new signal-slot connection merely to add a single static button or link to a table item at table definition time is nonsensical. Honestly, StackOverflow should just default to dynamically marking the answer currently receiving the most upvotes as "accepted." – Cecil Curry Feb 28 '18 at 08:10