user3739922
user3739922

Reputation: 100

Acquire row data from QTableView

In my c++ gui app with qt,

I have a button on my mainwindow, pressing which a dialog will open, in that dialog I have setup a QTableView which gets populated with data received from Oracle database.

QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");
db.setHostName("....");
db.setDatabaseName("...");
db.setUserName("...");
db.setPassword("...");
db.setPort(1521);

if(db.open())
{
    qDebug()<<"OPEN SUCCESS";
}
else
{
    qDebug()<<"ERROR "<<db.lastError().text();
}

this->model_oracle=new QSqlQueryModel();
model_oracle->setQuery("select * from TEST_1");
ui->tableView->setModel(model_oracle);

sample data output in tableview is shown below--

Name    Address    Age
Mike    NYC        25
Jim     FLA        39
Kate    SF         21

Now, when the user, clicks on any cell, I want the 3 fields of the row saved in 3 String variables. These 3 values of string variable I would use to populate data in my mainwindow form.

Any idea how to achieve this using QTableWidget structure i can do this with the cellClicked(int,int) signal. But I can not find something similar in QTableView.

I saw a question in SO that addressed something similar, but when I tried to use the same (i.e. cell wise) it still did not work.

Upvotes: 3

Views: 219

Answers (1)

user1092803
user1092803

Reputation: 3277

Try to connect the QAbstractItemView::clicked(QModelIndex) signal,

connect(ui->tableView, SIGNAL(clicked(QModelIndex)),this, SLOT(GetField()));

which should return the current item clicked.

Once you have the item, in the slot GetField(QModeilIndex index) you can access the selected row with something like:

row = index.row()

and then you should be able to access your field with

field = model_oracle->record(row)->field(..)

Upvotes: 2

Related Questions