PDA

View Full Version : Displaying a QPushButton in a QTableView



bytefish
28th March 2011, 14:32
Hi,

I want to display a QPushButton next to a row in a QSqlTableModel. The approach I have taken is using the setIndexWidget method and mapping the Button to the row with a QSignalMapper:



int column = model->columnCount();
model->insertColumn(column);
QSignalMapper *sigMap = new QSignalMapper(this);
QPushButton *button;
for(int i = 0; i < model->rowCount(); i++) {
button = new QPushButton(ui->tblViewClient);
button->setText("Show Details");
connect(button, SIGNAL(clicked()), sigMap, SLOT(map()));
sigMap->setMapping(button, model->data(model->index(i,0)).toInt());
ui->tblViewClient->setIndexWidget(model->index(i,column), button);
}
connect(sigMap, SIGNAL(mapped(int)), this, SLOT(showDetails(int)));


This works fine for me. But... On double clicking a row I am displaying the models data with a QDataWidgetMapper. If I close this dialog the model emits a dataChanged signal, the tableView gets rerendered and my buttons get lost (because I am using a QSqlTableModel...).

My question is, can you think of a simple way to avoid this? Connecting to dataChanged and repainting the whole thing does not help.

I hope my first post here is not too weird. :-)

Kind regards,
Philipp.

wysota
28th March 2011, 14:57
Please search the forum. This has been discussed here many times.

bytefish
28th March 2011, 15:28
So a solution would be to write a Delegate for the Button, like discussed in this post (http://www.qtcentre.org/threads/26916-inserting-custom-Widget-to-listview) and subclass my QSqlTableModel to add an extra column for the Button?

wysota
28th March 2011, 15:45
That's one of the possibilities.