Results 1 to 8 of 8

Thread: Can you share an event with two widget?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Can you share an event with two widget?

    Hi,

    I have two Graphicsview widgets that I display two images on them:
    ui->widget1->Scene->addPixmap(pix1);
    ui->widget2->Scene->addPixmap(pix2);
    In my graphicsview class I created a mouse wheelEvent that zoom the image in the graphicsview.
    I would like to be able to use my mouse wheel on one of the graphicsview widget and both images in widget1 and widget2 will zoom at the same time. Is it possible?

    Thanks in advance.

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

    Default Re: Can you share an event with two widget?

    If it is a viewport transformation then you can apply the same transformation on both views.
    Same for scene or item transformations.

    You can also use the same scene in two views if you like

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can you share an event with two widget?

    I am not sure I understand. The zooming event is associated to the widget where the mouse event occurs. How do I access the other widget ? ui is not recognized in this scope.
    The only way I can think is to emit a signal in the mouse wheel event which will be received by slots at the two widget.
    Can you be more specific in your answer?

    Thanks

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

    Default Re: Can you share an event with two widget?

    A cross widget signal/slot connection sounds good.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can you share an event with two widget?

    Hi,

    Zooming using signal/slot connection is working fine. When I try to do the same thing with panning I am getting the error:

    "invalid use of incomplete type class QScrollBar ...hBar->setValue()..."

    My code is:
    Qt Code:
    1. void MyGraphicsView::mousePressEvent(QMouseEvent * e)
    2. {
    3. if (e->buttons().testFlag(Qt::MidButton))
    4. {
    5. .
    6. .
    7. .
    8. }
    9. if (e->buttons().testFlag(Qt::LeftButton))
    10. {
    11. _lastPos = e->pos();
    12. QGraphicsView::mousePressEvent(e);
    13. }
    14. }
    15. void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
    16. {
    17. if (event->buttons().testFlag(Qt::LeftButton))
    18. {
    19. QPoint delta = event->pos() - _lastPos;
    20. _lastPos = event->pos();
    21. emit panning_signal(delta);
    22. }
    23. QGraphicsView::mouseMoveEvent(event);
    24. }
    25.  
    26. QObject::connect(ui->widget1, SIGNAL(panning_signal(QPoint )), this, SLOT(panning(QPoint )));
    27. QObject::connect(ui->widget2, SIGNAL(panning_signal(QPoint )), this, SLOT(panning(QPoint )));
    28. void MainWindow::panning(QPoint delta)
    29. {
    30. QScrollBar *hBar = ui->widget1->horizontalScrollBar();
    31. QScrollBar *vBar = ui->widget1->verticalScrollBar();
    32.  
    33. hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
    34. vBar->setValue(vBar->value() - delta.y());
    35.  
    36. hBar = ui->widget2->horizontalScrollBar();
    37. vBar = ui->widget2->verticalScrollBar();
    38.  
    39. hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
    40. vBar->setValue(vBar->value() - delta.y());
    41. }
    To copy to clipboard, switch view to plain text mode 
    I tried before to make the panning with the translate function and it was not working either (no error but nothing was happening).
    Any idea?

    Thanks
    Last edited by anda_skoa; 4th May 2014 at 18:29. Reason: missing [code] tags

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

    Default Re: Can you share an event with two widget?

    Quote Originally Posted by rakefet View Post
    Zooming using signal/slot connection is working fine. When I try to do the same thing with panning I am getting the error:

    "invalid use of incomplete type class QScrollBar ...hBar->setValue()..."
    Have you got the include for QScrollBar?

    Cheers,
    _

  7. #7
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can you share an event with two widget?

    Yes. Adding QScrollBar.h made it work. Thank you so much!

  8. #8
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: Can you share an event with two widget?

    I suggest you use a eventFilter() and check if the wheelEvent comes to any of the pix. then propagate the event via signals as you do. This seems more clean and easy to add new functionality later.

Similar Threads

  1. Forward key event to other widget?
    By Boron in forum Qt Programming
    Replies: 3
    Last Post: 6th February 2012, 14:55
  2. Replies: 3
    Last Post: 2nd August 2011, 21:15
  3. tree widget item's widget don't respond to any event.
    By quantity in forum Qt Programming
    Replies: 0
    Last Post: 11th April 2011, 04:30
  4. Replies: 7
    Last Post: 14th January 2010, 08:47
  5. Widget enter event
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 20th April 2008, 22:40

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
  •  
Qt is a trademark of The Qt Company.