how to connect to a slot when a row is selected in TableView?
Here's the code i've written so far:
Code:
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->show();
When a row is selected in the table view, i want to connect it to a slot.
How to do that? Thanks.
Re: how to connect to a slot when a row is selected in TableView?
See QAbstractItemView::selectionModel() to get the selection model of the view and then use its signals. See QItemSelectionModel for more informations.
Re: how to connect to a slot when a row is selected in TableView?
Re: how to connect to a slot when a row is selected in TableView?
Quote:
Originally Posted by
babygal
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.)
Re: how to connect to a slot when a row is selected in TableView?
I added the code below and tried but no signal emitted.And nothing happens.
Code:
Code:
public slots:
void querydb();
void myClass::querydb()
{
qDebug() << "querydb() Row selected:" << "1";
QMessageBox::about(this, tr
("Database row selected"),tr
("OK"));
}
Re: how to connect to a slot when a row is selected in TableView?
is
Code:
view->selectionModel()
returning a valid pointer? Did you have rerun qmake, do you use the Q_OBJECT macro?
Re: how to connect to a slot when a row is selected in TableView?
Quote:
Originally Posted by
Lykurg
is
Code:
view->selectionModel()
returning a valid pointer? Did you have rerun qmake, do you use the Q_OBJECT macro?
How to check these two?
Re: how to connect to a slot when a row is selected in TableView?
I think it is returning a valid pointer. And yes I'm using Q_OBJECT macro.
Re: how to connect to a slot when a row is selected in TableView?
Did you get any warnings on the console when starting the program?
Re: how to connect to a slot when a row is selected in TableView?
I'm pretty sure that the
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.
Re: how to connect to a slot when a row is selected in TableView?
Quote:
Originally Posted by
Live
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.
Re: how to connect to a slot when a row is selected in TableView?
Quote:
Originally Posted by
Lykurg
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.
Re: how to connect to a slot when a row is selected in TableView?
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);