Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-4514

QWidget::SetCursor does not change the cursor if a context menu is shown.

    XMLWordPrintable

Details

    Description

      Attached is some sample code, which, when compiled, brings up a simple line edit widget. The mouse cursor changes every five seconds just fine between Qt::SizeVerCursor and Qt::SizeHorCursor via a timer callback.

      Pressing the right button in the widget changes the cursor temporarily to Qt::WaitCursor.

      However, if you right click to bring up the context menu, then move the mouse away from the menu but still over the widget, then stop moving the mouse, the cursor does not update, even though calls are still being made to setCursor via the timer.

      Example code::

      #include <QtGui>
      
      namespace local
      {
        class LineEdit : public QLineEdit
        {
        public:
      
          LineEdit();
      
          virtual void mousePressEvent(QMouseEvent *MouseEvent);
          virtual void mouseReleaseEvent(QMouseEvent *MouseEvent);
          virtual void timerEvent(QTimerEvent *TimerEvent);
      
        private:
          int m_timerId;
          Qt::CursorShape myCursorShape;
        };
      }
      
      local::LineEdit::LineEdit()
      : QLineEdit(),
        myCursorShape(Qt::IBeamCursor)
      {
        // Change the cursor every two seconds.
        // You need to keep the timer id here so that you change the cursor correctly.
        m_timerId = startTimer(2000);
      }
      
      void local::LineEdit::mousePressEvent(QMouseEvent *MouseEvent)
      {
        qDebug("In local::LineEdit::mousePressEvent");
      
        if (MouseEvent->button() == Qt::RightButton)
        {
          setCursor(Qt::WaitCursor);
        }
      }
      
      void local::LineEdit::mouseReleaseEvent(QMouseEvent *MouseEvent)
      {
        qDebug("In local::LineEdit::mouseReleaseEvent");
      
        if (MouseEvent->button() == Qt::RightButton)
        {
          setCursor(Qt::IBeamCursor);
        }
      }
      
      void local::LineEdit::timerEvent(QTimerEvent *timerEvent)
      {
          qDebug() << "TimerEvent" << timerEvent->timerId();
          if ( m_timerId == timerEvent->timerId())
          {
              myCursorShape = (myCursorShape == Qt::IBeamCursor)
                  ? Qt::SizeHorCursor
                  : (myCursorShape == Qt::SizeHorCursor
                      ? Qt::SizeVerCursor : Qt::SizeHorCursor);
      
              qDebug() << "I am now changing the cursor";
              setCursor(myCursorShape);
          }
      }
      
      int main(int Argc, char *Argv[])
      {
        QApplication a(Argc, Argv);
      
        QMainWindow *mainWindow = new QMainWindow();
      
        mainWindow->setCentralWidget(new local::LineEdit());
        mainWindow->show();
      
        return a.exec();
      }
      
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            xcm Martin Petersson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes