Results 1 to 13 of 13

Thread: Signals & Slots question

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Signals & Slots question

    Hi

    I'm struggling to get certain signal and slot combinations to work. I've used signals and slots successfully for a while now but at the moment I'm stuck with drag & drop operations. I want to be able to drag from a QTableView object and I connect the necessary signals in the way shown below:

    Qt Code:
    1. connect(tableViewWorkspace,SIGNAL(mousePressEvent(QMouseEvent *event)),this,SLOT(mousePressEventWorkspace(QMouseEvent *event)));
    To copy to clipboard, switch view to plain text mode 

    This does not seems to work and when I click in the table view it never enters the mousePressEventWorkspace function. Do I do something wrong? I know that mousePressEvent is a function of the QAbstractItemView class, but shouldn't it be called for the tableView object as well? Do I perhaps need some type of pointer along with some object casting?

    Thanks
    Jaco
    Last edited by jpn; 22nd August 2008 at 09:29. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals & Slots question

    1. Have you seen our FAQ?

    http://www.qtcentre.org/forum/faq.ph...lot_with_names

    2. events are not signals nor slots, so your connect statement has no chance to work even if you correct the issue mentioned in (1). If you want some method to be called when an event handler is executed, simply reimplement the event and call you method there or use event filters.

  3. The following user says thank you to wysota for this useful post:

    JPNaude (22nd August 2008)

  4. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals & Slots question

    connect(tableViewWorkspace,SIGNAL(mousePressEvent( QMouseEvent *event)),this,SLOT(mousePressEventWorkspace(QMouse Event *event)));
    at first, there is no signal with name mousePressEvent.
    at second, you mustn't specify parameters.

    read this for details
    http://doc.trolltech.com/4.4/signalsandslots.html

  5. The following user says thank you to spirit for this useful post:

    JPNaude (22nd August 2008)

  6. #4
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Signals & Slots question

    Thanks wysota.

    I missed the fact that the events are not signals by default. It might seem like a stupid question, but I'm not sure how to reimplement the event. Should I subclass the QTableView class and implement these functions there and use this class rather than the current QTableView (I thought about this but I thought there should be an easier way), or how is it possible to reimplement the events in the main window class?

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals & Slots question

    You can either subclass the table and reimplement its event handler or install an event filter on it so that all its events are passed to another object before reaching the destination (see QObject::installEventFilter).

  8. The following user says thank you to wysota for this useful post:

    JPNaude (22nd August 2008)

  9. #6
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Signals & Slots question

    Hi again Wysota

    The event filter works well and I'm able to track all the events that happen for any control now. The problem now is that none of the mouse events are triggered for a tableView object. My event filter looks as follows:

    Qt Code:
    1. bool mainWindow::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj == this->tableViewWorkspace) {
    4. if (event->type() == QEvent::MouseButtonPress) {
    5. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    6. if (mouseEvent->button() == Qt::LeftButton)
    7. // Commands
    8. return false;
    9. } else if (event->type() == QEvent::MouseMove) {
    10. // etc.
    To copy to clipboard, switch view to plain text mode 

    I log all these events (code for that not shown above) and the eventFilter function is never called for an tableView object. For example, if I press the right mouse button it issues QEvent::Context menu, but never QEvent::MouseButtonPress.

    There is probably a reason for this, since the mouse press event is called for other objects in my GUI.

    Does anyone know what the reason is for that?
    Last edited by wysota; 22nd August 2008 at 11:47. Reason: missing [code] tags

  10. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Signals & Slots question

    after handeling of event you should return true if you don't need that parent widget process handled event and false in another case.

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals & Slots question

    Did you use installEventFilter()? Also don't return false here. Return QMainWindow::eventFilter(obj, event) instead.

  12. #9
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Signals & Slots question

    Thanks, I changed the return value now.

    However this does not solve the problem. I did install the event filter for the QTableView correctly because it logs all sorts of events for it (paint, focus related events etc.). The eventFilter does not get called for mouse related events. To verify this I've done the following:

    Qt Code:
    1. bool mainWindow::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (event->type() == 2) {
    4. loggerSingleton::Instance()->addDebugString("found a key press for object " + obj->objectName(),this);
    5. this->updateMessagesAndLogs();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Now when I click on different GUI objects for which I've installed this filter the debug message get locked. However when I click on the table view, it does not log this message so the eventFilter function is not called. It does not make sense to me at all since the keyPressEvent documentation for the QTableView does not state anything unusual.
    Last edited by wysota; 22nd August 2008 at 12:11. Reason: missing [code] tags

  13. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals & Slots question

    You probably want to install the event filter on the viewport() of the table...

  14. #11
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Thumbs up Re: Signals & Slots question

    Yes! Finally. Thanks it works now.

  15. #12
    Join Date
    Jul 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals & Slots question

    Quote Originally Posted by JPNaude View Post
    Yes! Finally. Thanks it works now.
    How did you solve it?
    I get this problem too. Can you post the codes?

  16. #13
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Signals & Slots question

    Qt Code:
    1. bool mainWindow::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. /* if (event->type() == QEvent::DragEnter) {
    4. loggerSingleton::Instance()->addDebugString("found a drag enter for object " + obj->objectName(),this);
    5. this->updateMessagesAndLogs();
    6.   }*/
    7.  
    8. if ((obj == this->tableViewWorkspace->viewport()) || (obj == this->tableViewWorkspace)) {
    9. this->updateMessagesAndLogs();
    10. if (event->type() == QEvent::MouseButtonPress) {
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  2. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  3. signals and slots in plugins
    By anderl in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 13:57
  4. newbe question about signals / slots
    By Walsi in forum Qt Programming
    Replies: 19
    Last Post: 20th April 2007, 10:54
  5. Signals and Slots question
    By Thoosle in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2006, 00:24

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.