Qt World Summit: Register Today!

How can we get current cursor position in any part of window from the child class event?

  • we have a class
    @ class custom::public QObject
    custom(QWidget *parent)

                                                   bool event (QEvent *e)
                                                   //get cursor position.

    suppose we have the above child class. Inside child class event how can we get cursor position irrespective of whether the cursor is
    on the parent or on the other child of same parent. I have inherited from QObject because i dont want to create any widget in this class just i want to know cursor position to handle other objects.

  • Hi pratik041,

    it would be nice, if you would start your code in the beginning of the line, not after half of the line.

    If you really get the event (how? if it is no widget, and which events do you want to get?), you could use the class "QCursor":http://doc.qt.nokia.com/4.7/qcursor.html and it's methods.

  • This is my code @

    #include <QtGui>
    #include <QPoint>

    custom::custom(QRect rect, QString msg,QWidget *w, QWidget *parent) :
    obj = w;
    m_msg = msg;
    m_rect = rect;

    bool custom::event ( QEvent * event )
    // I want to check here whether current cursor pos is under the given m_rect.
    // The m_rect can be any area of the parent window or any other child widget of the same parent.
    // I want to check this because based on that i have to do operation on obj.



    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPushButton>

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    QPushButton *button = new QPushButton(this);
    p = new custom(QRect(30,40,45,55), "msg",button,this);


    delete p;
    delete ui;

    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;
    return a.exec();


  • If you really want to keep this architecture, the "installEventFilter()":http://doc.qt.nokia.com/4.7/qobject.html#installEventFilter is the solution.

  • The point is, a QObject only gets events that are sent directly to him. They are no visible objects.
    Event propagation (e.g. mouse, keyboard) has the following flow:

    The widget that has the current focus, it's base class up to QObject, then the parent widget and there to the base class down to QObject, ...

    Custom childs will never get an event, if they are no widgets and this will not get events of other widgets.

    If yopu want to catch events in custom, you must use an eventFilter, as proposed by BilbonSacquet.

  • did you mean to implement event filter in parent window or child class?

  • Your event filter IS the custom class:

    • add a function bool custom::eventFilter(QObject *obj, QEvent *event)
    • install the filter: button->installEventFilter(custom);
    • in eventFilter check the message coming from button:
      @if (obj == button && event->type() == QEvent::MouseMove) {}@

    But my opinion the best way to overload in the parent of all your widgets the function event()!

  • [quote author="BilbonSacquet" date="1321864048"]But my opinion the best way to overload in the parent of all your widgets the function event()![/quote]

    This only makes sense, if you are only interested in not handled events. handled events will not reach the parent. If you want to react on the events, before they are processed, the event filter is the only option.

    By the way, there is also an option to install a global event filter on the global application object (QCoreApplication/QApplication)

  • My bad ... you have right!

Log in to reply