Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can we get current cursor position in any part of window from the child class event?

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 8.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    we have a class
    @ class custom::public QObject
    {
    Q_OBJECT
    public:
    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.

    Pratik Agrawal

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pratik041
        wrote on last edited by
        #3

        This is my code @
        object.cpp

        #include <QtGui>
        #include <QPoint>

        custom::custom(QRect rect, QString msg,QWidget *w, QWidget *parent) :
        QObject(parent),
        m_rect(rect),
        m_msg(msg)
        {
        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.

        }

        mainwindow.cpp

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

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        QPushButton *button = new QPushButton(this);
        button->setGeometry(30,40,100,70);
        p = new custom(QRect(30,40,45,55), "msg",button,this);

        }

        MainWindow::~MainWindow()
        {
        delete p;
        delete ui;
        }

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

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

        @

        Pratik Agrawal

        1 Reply Last reply
        0
        • BilbonSacquetB Offline
          BilbonSacquetB Offline
          BilbonSacquet
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            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.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pratik041
              wrote on last edited by
              #6

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

              Pratik Agrawal

              1 Reply Last reply
              0
              • BilbonSacquetB Offline
                BilbonSacquetB Offline
                BilbonSacquet
                wrote on last edited by
                #7

                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()!

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  [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)

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • BilbonSacquetB Offline
                    BilbonSacquetB Offline
                    BilbonSacquet
                    wrote on last edited by
                    #9

                    My bad ... you have right!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved