Results 1 to 10 of 10

Thread: Connect the QGrapchisScene of QGraphicsView click signal

  1. #1
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Connect the QGrapchisScene of QGraphicsView click signal

    I have a QGraphicsView and want to detect user clicks, but i have a problem, clickng the scrollbars also emit the click signal. How could i avoid the signal when scrollbars are clicked?

    thsi is my code:
    Qt Code:
    1. rs_Graphicsview::rs_Graphicsview(QWidget *parent) :
    2. QGraphicsView(parent)
    3. {
    4. installEventFilter(this);
    5. }
    6.  
    7.  
    8. bool rs_Graphicsview::eventFilter(QObject *obj, QEvent *evnt) {
    9. if (evnt->type() == QEvent::MouseButtonPress) {
    10. emit clicked();
    11.  
    12. }
    13. return QObject::eventFilter(obj, evnt);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Always trying to learn >.<

  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: Connect the QGrapchisScene of QGraphicsView click signal

    Install the event filter on the view's viewport().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Connect the QGrapchisScene of QGraphicsView click signal

    What do want to achieve my getting user click on the QGraphicsView?

    Install the event filter on the viewport
    Qt Code:
    1. installEventFilter(this->viewport());
    To copy to clipboard, switch view to plain text mode 

    Most of the cases it is more useful to receive (filter) on the QGraphicsScene?

    Edit: I should improve my keyboard skills (it took ~10 min to type this )
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Connect the QGrapchisScene of QGraphicsView click signal

    Using
    Qt Code:
    1. installEventFilter(this->viewport());
    To copy to clipboard, switch view to plain text mode 
    desn't work.

    Just stops emitting clicked() signal.
    Always trying to learn >.<

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Connect the QGrapchisScene of QGraphicsView click signal

    try this
    Qt Code:
    1. bool rs_Graphicsview::eventFilter(QObject *obj, QEvent *evnt) {
    2. if (evnt->type() == QEvent::MouseButtonPress) {
    3. emit clicked();
    4.  
    5. }
    6. //return QObject::eventFilter(obj, evnt);
    7. return false;
    8. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Connect the QGrapchisScene of QGraphicsView click signal

    It's strange, just does the opposite, works on scrollbar clicks, and ignores scene area clicks
    Always trying to learn >.<

  7. #7
    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: Connect the QGrapchisScene of QGraphicsView click signal

    That's because you didn't install the filter on the correct object.

    It should be:
    Qt Code:
    1. view->viewport()->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Connect the QGrapchisScene of QGraphicsView click signal

    still emitting when scrolbar clicked, i must be clumsy, seems easy but something fails
    Always trying to learn >.<

  9. #9
    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: Connect the QGrapchisScene of QGraphicsView click signal

    Well... it works just fine for me...

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Monitor : public QObject {
    4. public:
    5. Monitor(QObject *parent = 0) : QObject(parent) {}
    6.  
    7. protected:
    8. bool eventFilter(QObject *o, QEvent *e) {
    9. if(e->type() == QEvent::MouseButtonPress) {
    10. qDebug() << "CLICK";
    11. }
    12. return false;
    13. }
    14. };
    15.  
    16. int main(int argc, char **argv) {
    17. QApplication app(argc, argv);
    18. QGraphicsScene scene(0,0,2000,2000);
    19. view.setScene(&scene);
    20. Monitor monitor;
    21. view.viewport()->installEventFilter(&monitor);
    22. view.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Connect the QGrapchisScene of QGraphicsView click signal

    Yes there is mistake in my post
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 0
    Last Post: 11th July 2012, 04:50
  2. Connect signal/signal in Qt Designer
    By jlemaitre in forum Newbie
    Replies: 1
    Last Post: 22nd September 2010, 15:53
  3. Simulating a Button Click signal
    By Ratheendrans in forum Qt Programming
    Replies: 4
    Last Post: 5th May 2010, 11:57
  4. QDateEdit and mouse click signal
    By ouekah in forum Newbie
    Replies: 2
    Last Post: 23rd February 2010, 19:00
  5. Click SIGNAL on QFrame
    By sosanjay in forum Qt Programming
    Replies: 2
    Last Post: 8th December 2009, 09:14

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.