Getting row data from QTableView

In my C ++ gui app with qt,

I have a button on my main window which will open with a dialog box, in this dialog I have set a QTableView which is populated with data retrieved 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);

      

the sample data output as a table 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 to be stored in 3 string variables. These 3 values ​​of the string variable i will be used to populate the data in my mainwindow form.

Any idea how to achieve this ??? using the QTableWidget structure, I can do it with a signal cellClicked(int,int)

. But I cannot find something like this in the QTableView.

I saw a question on SO that referred to something similar, but when I tried to use the same (i.e. cell) it still didn't work.

+3


source to share


1 answer


Try to connect a signal QAbstractItemView::clicked(QModelIndex)

,

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

      

which should return the current element.

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(..)

      

+2


source







All Articles