selectionChanged() signal in qtableview
I am trying to get an item from qtableview or specifically a column from the selected row when a raw selected.
this is my code
Code:
db.setDatabaseName("memory");
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
//return false;
}
model->setTable("person");
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Horizontal, tr("Name"));
model->setHeaderData(2, Qt::Horizontal, tr("House Name"));
ui->tableView->setModel(model);
ui->tableView->show();
connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged()), this, SLOT(on_tableViewSelection()));
error shown is
"QObject::connect: No such signal QItemSelectionModel::selectionChanged() in ../CDMS/cdms.cpp:43
QObject::connect: (receiver name: 'CDMS')"
Re: selectionChanged() signal in qtableview
As the error message says, there is no signal named selectionChanged for the QItemSelectionModel. I suspect you want the same signal for QTableView, so change your connect statement to:
Code:
connect(ui->tableView, SIGNAL(selectionChanged()), this, SLOT(on_tableViewSelection()));
Re: selectionChanged() signal in qtableview
The selectionChanged signal of the QItemSelectionModel has two arguments. They are missing in the SIGNAL() argument of your connect().
Cheers,
_
Re: selectionChanged() signal in qtableview
Changed the code like this but still got error message
Code:
connect(ui->tableView->selectionModel(),
this,
error: macro "SIGNAL" passed 3 arguments, but takes just 1
error: 'SIGNAL' was not declared in this scope
Thanks.
Re: selectionChanged() signal in qtableview
You have unbalanced parenthesis for the SIGNAL macro, right?
Re: selectionChanged() signal in qtableview
thanks, I am an idiot, sorry for wasting your time
corrected code
Code:
db.setDatabaseName("memory");
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
//return false;
}
model->setTable("person");
//model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Horizontal, tr("Name"));
model->setHeaderData(2, Qt::Horizontal, tr("House Name"));
ui->tableView->setModel(model);
ui->tableView->show();
connect(ui->tableView->selectionModel(),
this,
Re: selectionChanged() signal in qtableview
No problem, we've all been there... :) That is just one reason why I prefer the new connect syntax that avoids the use of SIGNAL/SLOT macros. The equivalent for your case would be:
Code:
connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &WhateverYourClassIsNamed::on_tableViewSelection);
Let the compiler do the work for you and catch parameter mismatches, etc.
Re: selectionChanged() signal in qtableview
Quote:
Originally Posted by
jthomps
No problem, we've all been there... :) That is just one reason why I prefer the new connect syntax that avoids the use of SIGNAL/SLOT macros.
Indeed, but that became available in Qt5, according to breakthecode's profile they are using Qt4.
It is possible to make the connect a bit less verbose though, i.e. removing the const references
Code:
connect(ui->tableView->selectionModel(),
this,
Also, if your slot doesn't need the arguments, you can use a slot with fewer arguments than the signal has., e.g.
Code:
connect(ui->tableView->selectionModel(),
this,
SLOT(on_tableViewSelection()));
Cheers,
_