Results 1 to 13 of 13

Thread: how to connect to a slot when a row is selected in TableView?

  1. #1
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default how to connect to a slot when a row is selected in TableView?

    Here's the code i've written so far:
    Qt Code:
    1. QTableView *view;
    2.  
    3. model = new QSqlQueryModel;
    4. view = new QTableView;
    5.  
    6. 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 + "'");
    7. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Scan Date"));
    8. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Surname"));
    9. model->setHeaderData(2, Qt::Horizontal, QObject::tr("First name"));
    10. model->setHeaderData(3, Qt::Horizontal, QObject::tr("NRIC"));
    11. model->setHeaderData(4, Qt::Horizontal, QObject::tr("DOB"));
    12. model->setHeaderData(5, Qt::Horizontal, QObject::tr("Gender"));
    13. model->setHeaderData(6, Qt::Horizontal, QObject::tr("Race"));
    14.  
    15. view->setModel(model);
    16. view->setSelectionBehavior(QAbstractItemView::SelectRows);
    17. view->setSelectionMode(QAbstractItemView::SingleSelection);
    18.  
    19.  
    20. view->show();
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

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

  3. #3
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect to a slot when a row is selected in TableView?

    Any code example.please.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to connect to a slot when a row is selected in TableView?

    Quote Originally Posted by babygal View Post
    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.)

  5. #5
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

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

    Qt Code:
    1. public slots:
    2. void querydb();
    3.  
    4. connect(view->selectionModel(),SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this,SLOT(querydb()));
    5.  
    6. void myClass::querydb()
    7. {
    8. qDebug() << "querydb() Row selected:" << "1";
    9. QMessageBox::about(this, tr("Database row selected"),tr("OK"));
    10. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to connect to a slot when a row is selected in TableView?

    is
    Qt Code:
    1. view->selectionModel()
    To copy to clipboard, switch view to plain text mode 
    returning a valid pointer? Did you have rerun qmake, do you use the Q_OBJECT macro?

  7. #7
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect to a slot when a row is selected in TableView?

    Quote Originally Posted by Lykurg View Post
    is
    Qt Code:
    1. view->selectionModel()
    To copy to clipboard, switch view to plain text mode 
    returning a valid pointer? Did you have rerun qmake, do you use the Q_OBJECT macro?
    How to check these two?

  8. #8
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

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

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

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

  10. #10
    Join Date
    Sep 2010
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect to a slot when a row is selected in TableView?

    I'm pretty sure that the

    Qt Code:
    1. connect(view->selectionModel(),SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this,SLOT(querydb()));
    To copy to clipboard, switch view to plain text mode 

    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to connect to a slot when a row is selected in TableView?

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

  12. #12
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to connect to a slot when a row is selected in TableView?

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

  13. #13
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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);

Similar Threads

  1. Replies: 3
    Last Post: 6th October 2010, 08:33
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. connect a qpushbutton a slot
    By Lycus HackerEmo in forum Newbie
    Replies: 13
    Last Post: 29th March 2010, 09:14
  4. QObject::connect: No such slot !?!
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 18:31
  5. Qt Designer & Qt4, connect to my own slot.
    By geitosten in forum Newbie
    Replies: 2
    Last Post: 17th February 2007, 19:22

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.