PDA

View Full Version : TableView store contents of particular cell in variable



Splatify
15th February 2011, 22:06
Ok I have a table populated with data from my database.

What i want is when a user selects a cell and a button is pressed, the data within the cell they selected is used in a function as a variable. I have no idea how to achieve this. Could someone please point me in the right direction.

Thanks for your time and trouble :D

ChrisW67
15th February 2011, 22:27
In the slot reacting to the button, use the view's QItemSelectionModel to determine the current cell's QModelIndex. Then retrieve the value of the Qt::DisplayRole (or whatever) for that index from the model (not the view).

Splatify
16th February 2011, 09:15
Could you perhaps give me some example code? I'm new at this and really confused.








UserWindow::UserWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::UserWindow)
{
//Other code here

query.exec(querystring);
QSqlQueryModel *model = new QSqlQueryModel;

model->setQuery(query);
model->removeColumn(0);
model->removeColumn(1);
model->setHeaderData(0, Qt::Horizontal, "Name");
model->setHeaderData(1, Qt::Horizontal, "Date");
model->setHeaderData(2, Qt::Horizontal, "Completed");

// Populate the table with the relevant information from the database

ui->tableView->setModel(model);
ui->tableView->show();
}



void UserWindow::on_NextButton_clicked()
{


QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
// What do I do here?
// I want the value within the currently selected cell to be used here.
// Could someone give me some example code to demonstrate how to do this.

}


Thanks