PDA

View Full Version : selectionChanged() signal in qtableview



breakthecode
17th March 2015, 23:14
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


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
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"
"Click Cancel to exit."), QMessageBox::Cancel);
//return false;
}

QSqlTableModel *model = new QSqlTableModel;
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->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
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')"

jefftee
18th March 2015, 00:39
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:



connect(ui->tableView, SIGNAL(selectionChanged()), this, SLOT(on_tableViewSelection()));

anda_skoa
18th March 2015, 08:23
The selectionChanged signal of the QItemSelectionModel has two arguments. They are missing in the SIGNAL() argument of your connect().

Cheers,
_

breakthecode
18th March 2015, 21:49
Changed the code like this but still got error message


connect(ui->tableView->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &),
this,
SLOT(on_tableViewSelection(const QItemSelection &, const QItemSelection &)));

error: macro "SIGNAL" passed 3 arguments, but takes just 1

error: 'SIGNAL' was not declared in this scope

Thanks.

jefftee
18th March 2015, 22:03
You have unbalanced parenthesis for the SIGNAL macro, right?

breakthecode
19th March 2015, 00:28
thanks, I am an idiot, sorry for wasting your time

corrected code



QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
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"
"Click Cancel to exit."), QMessageBox::Cancel);
//return false;
}

QSqlTableModel *model = new QSqlTableModel;
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->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui->tableView->show();
connect(ui->tableView->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this,
SLOT(on_tableViewSelection(const QItemSelection &, const QItemSelection &)));

jefftee
19th March 2015, 00:32
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:



connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &WhateverYourClassIsNamed::on_tableViewSelection);


Let the compiler do the work for you and catch parameter mismatches, etc.

anda_skoa
19th March 2015, 07:47
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



connect(ui->tableView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
this,
SLOT(on_tableViewSelection(QItemSelection, QItemSelection)));

Also, if your slot doesn't need the arguments, you can use a slot with fewer arguments than the signal has., e.g.


connect(ui->tableView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
this,
SLOT(on_tableViewSelection()));


Cheers,
_