PDA

View Full Version : How to prevent opening the same record in a new window more than once in QTableView ?



zed220
29th September 2016, 11:51
Hi All,
I wonder if anyone can help, I have a QTableView where I'm displaying list of records, to display further details of any record I'm opening a new window by clicking on the record, all well apart from that I can open the SAME record more than once, How can I prevent this happening ?

this is what I have so far

while(qry.next())
{
QSqlQueryModel *model = new QSqlQueryModel();
model->setQuery(qry);
}
connect(ui->tableView_visitLog, SIGNAL(pressed(const QModelIndex&)), this,
SLOT(on_tableView_visitorLog_clicked(const QModelIndex&)));


void ControlPanel::on_tableView_visitorLog_clicked(cons t QModelIndex& index)
{
const QAbstractItemModel* tableModel = ui->tableView_visitLog->model();
int visitor_id = tableModel->data(tableModel->index(index.row(),0), Qt::DisplayRole).toInt();
visitordetails = new VisitorDetails(visitor_id, this);
visitordetails->show();
}

anda_skoa
29th September 2016, 17:09
Add a QHash member to your class that uses the visitor_id value as the key and has a pointer to the VisitorDetails as the value.

When you get into that slot you first check if you have an entry already and only create one if you don't.
If you have one you can still call show() and raise() on it to make it come to the front (if there are more than one of these open)

Cheers,
_