Results 1 to 8 of 8

Thread: selectionChanged() signal in qtableview

  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default 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

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("memory");
    3. if (!db.open()) {
    4. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    5. qApp->tr("Unable to establish a database connection.\n"
    6. "This example needs SQLite support. Please read "
    7. "the Qt SQL driver documentation for information how "
    8. "to build it.\n\n"
    9. "Click Cancel to exit."), QMessageBox::Cancel);
    10. //return false;
    11. }
    12.  
    13. model->setTable("person");
    14. model->select();
    15. model->setHeaderData(0, Qt::Horizontal, tr("ID"));
    16. model->setHeaderData(1, Qt::Horizontal, tr("Name"));
    17. model->setHeaderData(2, Qt::Horizontal, tr("House Name"));
    18. ui->tableView->setModel(model);
    19. ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    20. ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    21. ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    22. ui->tableView->show();
    23. connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged()), this, SLOT(on_tableViewSelection()));
    To copy to clipboard, switch view to plain text mode 

    error shown is
    "QObject::connect: No such signal QItemSelectionModel::selectionChanged() in ../CDMS/cdms.cpp:43
    QObject::connect: (receiver name: 'CDMS')"
    Last edited by breakthecode; 18th March 2015 at 00:02.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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:

    Qt Code:
    1. connect(ui->tableView, SIGNAL(selectionChanged()), this, SLOT(on_tableViewSelection()));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    breakthecode (19th March 2015)

  5. #4
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: selectionChanged() signal in qtableview

    Changed the code like this but still got error message

    Qt Code:
    1. connect(ui->tableView->selectionModel(),
    2. SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &),
    3. this,
    4. SLOT(on_tableViewSelection(const QItemSelection &, const QItemSelection &)));
    To copy to clipboard, switch view to plain text mode 

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

    error: 'SIGNAL' was not declared in this scope

    Thanks.
    Last edited by breakthecode; 18th March 2015 at 21:56.

  6. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: selectionChanged() signal in qtableview

    You have unbalanced parenthesis for the SIGNAL macro, right?

  7. The following user says thank you to jefftee for this useful post:

    breakthecode (19th March 2015)

  8. #6
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: selectionChanged() signal in qtableview

    thanks, I am an idiot, sorry for wasting your time

    corrected code

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("memory");
    3. if (!db.open()) {
    4. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    5. qApp->tr("Unable to establish a database connection.\n"
    6. "This example needs SQLite support. Please read "
    7. "the Qt SQL driver documentation for information how "
    8. "to build it.\n\n"
    9. "Click Cancel to exit."), QMessageBox::Cancel);
    10. //return false;
    11. }
    12.  
    13. model->setTable("person");
    14. //model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    15. model->select();
    16. model->setHeaderData(0, Qt::Horizontal, tr("ID"));
    17. model->setHeaderData(1, Qt::Horizontal, tr("Name"));
    18. model->setHeaderData(2, Qt::Horizontal, tr("House Name"));
    19. ui->tableView->setModel(model);
    20. ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    21. ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    22. ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    23. ui->tableView->show();
    24. connect(ui->tableView->selectionModel(),
    25. SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
    26. this,
    27. SLOT(on_tableViewSelection(const QItemSelection &, const QItemSelection &)));
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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:

    Qt Code:
    1. connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &WhateverYourClassIsNamed::on_tableViewSelection);
    To copy to clipboard, switch view to plain text mode 

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

  10. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: selectionChanged() signal in qtableview

    Quote Originally Posted by jthomps View Post
    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

    Qt Code:
    1. connect(ui->tableView->selectionModel(),
    2. SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
    3. this,
    4. SLOT(on_tableViewSelection(QItemSelection, QItemSelection)));
    To copy to clipboard, switch view to plain text mode 
    Also, if your slot doesn't need the arguments, you can use a slot with fewer arguments than the signal has., e.g.
    Qt Code:
    1. connect(ui->tableView->selectionModel(),
    2. SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
    3. this,
    4. SLOT(on_tableViewSelection()));
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Detect when the content of a cell of QTableView is changed
    By qt_developer in forum Qt Programming
    Replies: 5
    Last Post: 21st August 2021, 16:00
  2. Replies: 1
    Last Post: 29th May 2014, 05:16
  3. Value changed in a QTableView field
    By nittalope in forum Newbie
    Replies: 4
    Last Post: 12th August 2009, 09:21
  4. problem with changed() signal emitted by QGraphicsScene
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2009, 13:55
  5. Replies: 9
    Last Post: 23rd November 2006, 11:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.