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 to detect whether mouseEvents take place over this or not?
Forum Updated to NodeBB v4.3 + New Features

How to detect whether mouseEvents take place over this or not?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 506 Views 3 Watching
  • 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.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by deleted385
    #1

    When I press the mouse over this (ActionButton), it turns red and as I move mouse (while it's pressed) away from this, the leaveEvent doesn't fire. That's ok, I want to suppress the emit triggered() in the mouseReleaseEvent if it's released elsewhere (not over this). Here's what happened:

    x1.gif

    First, the Print button is disabled so it shouldn't change color, I wanted to make it light gray when it's disabled and reset the color on isEnabledChanged, looks like there's no such event to handle the effect of setEnabled, is there any?

    Second, when I moved mouse away, while it was pressed, from the plus icon on Objects section it was red and as soon as I released mouse emit triggered was called so the file dialog opened. I want it to open the file dialog if the mouse is released over that icon otherwise ignore. Here's what I've for now:

    ActionButton::ActionButton(const QString &fileName, const QString toolTip, int size, QWidget *parent)
        : QSvgWidget(parent)
    {
        QFile f(fileName);
        f.open(QIODevice::ReadOnly | QIODevice::Text);
        m_document.setContent(f.readAll());
        setToolTip(toolTip);
        m_isChecked = false;
        if(size == 0) setMaximumSize(QSize(16,16));
        else setMaximumSize(QSize(size, size));
        changeColor(QColor(Qt::black));
    }
    bool ActionButton::isChecked(){ return m_isChecked; }
    void ActionButton::setIsChecked(bool value){
        if(m_isChecked == value) return;
        m_isChecked = value;
        if(value) changeColor(Qt::blue);
        else changeColor(Qt::black);;
    }
    void ActionButton::enterEvent(QEnterEvent*){ if(!m_isChecked) changeColor(Qt::blue); }
    void ActionButton::leaveEvent(QEvent*){ if(!m_isChecked) changeColor(Qt::black); }
    void ActionButton::mousePressEvent(QMouseEvent*){ if(!m_isChecked) changeColor(Qt::red); }
    void ActionButton::mouseReleaseEvent(QMouseEvent*){ if(!m_isChecked) emit triggered(); }
    void ActionButton::changeColor(const QColor &color){
        m_document.elementsByTagName("path").at(0).toElement().setAttribute("fill", color.name());
        renderer()->load(m_document.toByteArray());
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      Then I would recommend taking a look at the various button class implementation directly and start with QAbstractButton.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Beside your SVG color customization, doesn't QPushButton provide the behaviour you want ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          Beside your SVG color customization, doesn't QPushButton provide the behaviour you want ?

          D Offline
          D Offline
          deleted385
          wrote on last edited by deleted385
          #3

          @SGaist, that's an entry point for understanding QWidget behavior, way of implementing custom functionalities, etc.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #4

            As @SGaist said, QPushButton or QAbstractButton already implements that behaviour.

            I want it to open the file dialog if the mouse is released over that icon otherwise ignore

            if(! hitButton(event->pos())) return;
            

            I wanted to make it light gray when it's disabled

            isEnabled() exists in QAbstractButton
            and isDown() for highlight the button when clicked inside.

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              Then I would recommend taking a look at the various button class implementation directly and start with QAbstractButton.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply
              1
              • SGaistS SGaist

                Then I would recommend taking a look at the various button class implementation directly and start with QAbstractButton.

                D Offline
                D Offline
                deleted385
                wrote on last edited by
                #6

                @SGaist, excellent. Needed that changeEvent to change color on enabled/disabled and for hit test rect().contains(releaseEvent->pos()) looks like working perfectly.

                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