Results 1 to 1 of 1

Thread: QRubberBand in child widgets

  1. #1
    Join Date
    Jun 2012
    Posts
    194
    Thanks
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QRubberBand in child widgets

    I can get QRubberBand to work when I provide mouseEvent overrides in my MainWindow widget. But, I want to get rectangle coordinates relative to a child widget's origin.

    Seems like this would be easier than getting the offset and scaling between the MainWindow?

    The centralWidget of the MainWindow is contained in another widget (ImageComposer->window). I tried implementing the mouse event overrides in the ImageComposer class, but the code doesn't execute:

    Qt Code:
    1. void ImageComposer::mousePressEvent(QMouseEvent *event)
    2. {
    3. //doingRB = false;
    4. origin = event->pos();
    5. //if (!rubberBand)
    6. rubberBand = new QRubberBand(QRubberBand::Rectangle, window);
    7. rubberBand->setGeometry(QRect(origin, QSize()));
    8. rubberBand->show();
    9. }
    10.  
    11. void ImageComposer::mouseMoveEvent(QMouseEvent *event)
    12. {
    13.  
    14. //doingRB = true;
    15. rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
    16. }
    To copy to clipboard, switch view to plain text mode 

    The same code runs as expected when located in MainWindow.

    Do I need to subclass a QWidget and implement the mouseEvent overrides in that new class? Then, instantiate the class with mouseEvent methods instead of an ordinary QWidget?

    Does this seem better than getting the rectangle coordinates in the mainWindow coordinate system then converting?

    Thanks!


    Added after 8 minutes:


    Subclassing a QWidget and implementing the mouseEvent methods in that class works.

    Is this the better way of getting the coordinates relative to the child widget's origin?
    Last edited by davethomaspilot; 23rd September 2021 at 22:33.

Similar Threads

  1. set an image behind all child widgets
    By quantumpilgim in forum Newbie
    Replies: 2
    Last Post: 4th September 2016, 13:17
  2. Child widgets
    By poporacer in forum Newbie
    Replies: 6
    Last Post: 12th August 2010, 04:04
  3. Replies: 5
    Last Post: 19th April 2010, 00:31
  4. How to get the child widgets from a Widget?
    By prykHetQuo in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2009, 14:26
  5. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 09:39

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.