Results 1 to 9 of 9

Thread: MousePressEvent in a child widget

  1. #1
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default MousePressEvent in a child widget

    Hello everybody,

    I created a child widget in a QMainWindow. In this widget, I overloaded the mousePressEvent but that not work.

    Here is the code:

    widget.h

    class myWidget : public QWidget {
    Q_OBJECT
    private:

    protected:
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);

    public:
    BoardWidget(QWidget *parent);
    ~BoardWidget();
    };

    widget cpp:

    void myWidget::
    mousePressEvent(QMouseEvent *event)
    {
    qDebug()<<"pass";
    }

    The widget is created in the constructor of my MainWindow:

    widget = new myWidget(this);
    widget->setGeometry(...);
    widget->show();

    I am using qt 4.7.3 on a mac.

    Thank you,

    Pat

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: MousePressEvent in a child widget

    What doesn't work?

  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: MousePressEvent in a child widget

    As I see, the most obvious reason, why it is not working is you'r not clicking on myWidget. Think again. May be your widget is very small to be under a mouse, or may be your widget is not visible at all. Try making your widget larger / visible enough so that you can click.

    Qt Code:
    1. widget = new myWidget(this);
    2. widget->setGeometry(...);
    3. widget->show();
    To copy to clipboard, switch view to plain text mode 
    not sure why your are using setGeomerty(), instead use size hinting. (this also may be a reason for why your are seeing)

    If there are any other widgets on mainwindow? they can overlap with your mywidget, you are just creating the mywidget with mainwindow are parent, not adding it to layout, or not setting it as central widget, hence there is a possibility that your mouse events are consumed by a widget which is over your mywidget, this is applicable if you have some other widgets on main window (as this is not very clear from your code)

  4. #4
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: MousePressEvent in a child widget

    I am clicking on myWidget. I removed from the sample code the paintEvent function in which a shape is drawn. Thus, I am sure that I am clicking on the widget.

    In order to resolve the problem, I also have overloaded the mousePressEvent in the main window and the function is called when I click in the widget (a call to myWidget->underMouse() shows that I clicked in the widget).

    I only have 1 widget.

    I have another program in which I use the same approach. It worked well until I upgraded to 4.7.3 but now I have the same problem (after recompilation). I know that the previous version was 4.x.

    Thanks

  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: MousePressEvent in a child widget

    In order to resolve the problem, I also have overloaded the mousePressEvent in the main window and the function is called when I click in the widget (a call to myWidget->underMouse() shows that I clicked in the widget).
    main window mousePressEvent() is called in 2 cases
    1. When you click on part of main window which is not occupied by the mywidget
    2. When you click on mywidget, and mywidget ignores the mouse click event

    which do you think is happening in your case

  6. #6
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: MousePressEvent in a child widget

    It's clear that the event is ignored by the child widget.

    I tried to create the widget without parent (widget = new myWidget(0)) and in this case, the event is captured correctly.

  7. #7
    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: MousePressEvent in a child widget

    How did you set the widget onto the QMainWindow?

    Do it this way, it should work.
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. ...
    4. widget = new myWidget(this);
    5. widget->setGeometry(...);
    6. widget->show();// Not required
    7. setCentralWidget(widget);
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: MousePressEvent in a child widget

    Using setCentralWidget works. But what happen if I want to add another widget (such as list box) in the main window?

    EDIT:

    I removed the line widget->show() and now it works correctly (without the setCentralWidget). I would like to understand this behavior...

    Thank you very much...

  9. #9
    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: MousePressEvent in a child widget

    QMainWindow can have only one central widget(fixed widget), and multiple docking widgets (movable widgets)

    If you want to have multiple widgets in central area, then you need to put all the widgets in another widget (say top widget), and then set this top widget as central widget.

    If you want widgets to be movable, then consider using QDockWidget instead of (or along with) central widget.

    Your problem:
    You just created the widget with mainwindow as parent, but was did not add to the main window layout formally, (either setCentralWidget() or addDockWidget()), hence may it may not have propagated the mouse events properly.

    Any ways the correct way to place a widget on main window is either setCentralWidget() or addDockWidget() (I should have posted this statement in my first reply)

    Also note that QMainWindow, has special way to handle the layouts, unlike other widgets, Refer QMainWindow Documenations for the exact layout

  10. The following user says thank you to Santosh Reddy for this useful post:

    PatC (7th June 2011)

Similar Threads

  1. Replies: 1
    Last Post: 11th March 2011, 19:34
  2. child widget resize to parent widget
    By bobFromAccounting in forum Newbie
    Replies: 10
    Last Post: 11th February 2011, 02:53
  3. Replies: 7
    Last Post: 14th January 2010, 08:47
  4. Replies: 4
    Last Post: 3rd October 2009, 08:19
  5. let parent widget grow with child widget?
    By BeS in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 11:17

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.