PDA

View Full Version : how to connect to a slot when a row is selected in TableView?



babygal
5th October 2010, 11:16
Here's the code i've written so far:

QSqlQueryModel *model;
QTableView *view;

model = new QSqlQueryModel;
view = new QTableView;

model->setQuery("SELECT s.scan_date,p.surname,p.first_name,p.nric_no,p.dob ,p.gender,r.description FROM PATIENT p,RACE r, SCAN_DATA s WHERE p.race = r.race AND p.patient_id=s.patient_id AND p.surname = '" + patientNamestr + "'");
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Scan Date"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Surname"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("First name"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("NRIC"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("DOB"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("Gender"));
model->setHeaderData(6, Qt::Horizontal, QObject::tr("Race"));

view->setModel(model);
view->setSelectionBehavior(QAbstractItemView::SelectRows );
view->setSelectionMode(QAbstractItemView::SingleSelectio n);


view->show();

When a row is selected in the table view, i want to connect it to a slot.
How to do that? Thanks.

Lykurg
5th October 2010, 11:23
See QAbstractItemView::selectionModel() to get the selection model of the view and then use its signals. See QItemSelectionModel for more informations.

babygal
6th October 2010, 03:34
Any code example.please.

Lykurg
6th October 2010, 07:14
Any code example.please.

Ok, then you posted in the wrong forum. Moved to newbie.

What have you tried so far? have you red the methods I mentioned? Read the documentation about signal and slots. There is no use if I post code because you have to learn it (also how to learn things right out of the docs.)

babygal
6th October 2010, 09:53
I added the code below and tried but no signal emitted.And nothing happens.
Code:


public slots:
void querydb();

connect(view->selectionModel(),SIGNAL(currentRowChanged(QModelIn dex,QModelIndex)),this,SLOT(querydb()));

void myClass::querydb()
{
qDebug() << "querydb() Row selected:" << "1";
QMessageBox::about(this, tr("Database row selected"),tr("OK"));
}

Lykurg
6th October 2010, 09:55
is
view->selectionModel()returning a valid pointer? Did you have rerun qmake, do you use the Q_OBJECT macro?

babygal
6th October 2010, 10:44
is
view->selectionModel()returning a valid pointer? Did you have rerun qmake, do you use the Q_OBJECT macro?

How to check these two?

babygal
7th October 2010, 11:36
I think it is returning a valid pointer. And yes I'm using Q_OBJECT macro.

Lykurg
7th October 2010, 12:50
Did you get any warnings on the console when starting the program?

Live
7th October 2010, 13:27
I'm pretty sure that the


connect(view->selectionModel(),SIGNAL(currentRowChanged(QModelIn dex,QModelIndex)),this,SLOT(querydb()));

will not work since your signal and your slot don't have the same parameters type. When you connect, you have to connect a signal to a slot with exactly the same parameters.

Hope this helps.

Lykurg
7th October 2010, 13:35
When you connect, you have to connect a signal to a slot with exactly the same parameters.That's not true. A slot can have the same parameter count as the signal or lesser but never more.

babygal
8th October 2010, 07:18
Did you get any warnings on the console when starting the program?

I didn't notice any serious warnings. But the matter got resolved when I combined the table view into my main dialog's layout.

qt_gotcha
8th October 2010, 16:19
I think the function must have QIndex in the header:
this works for me
connect(fileView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(selectFiles(QModelIndex)));

void nutshellqt::selectFiles(const QModelIndex& index)
{

}

make sure the function is in the header file under public slots

public slots:
void selectFiles(const QModelIndex& index);