-1

I implemented showing button when mouse is under QTextEdit in the QWidget.

when I test code, I found it sometimes didn't work showing button

bug cause is that MouseMoveEvent doesn't work in Qwidget when I move in the QTextEdit.

I don't understand why MouseMoveEvent doesn't work...

here is my code

this->setMouseTracking(true); // this is QWidget

ui.textEdit_log->setMouseTracking(true); // this is textEdit

void QWidget::mouseMoveEvent(QMouseEvent* event) // reImplemented mouseMoveEvent
{
    if (ui.textEdit_log->underMouse())
    {
        m_pButton->show();
    }
    else
    {
        m_pButton->hide();
    }
}
jeinne
  • 11
  • 3

3 Answers3

2

Mouse tracking for QTextEdit will only work by subclassing QTextEdit and reimplementing it's mouseMoveEvent() from where you can pass the event up to the parent's mouseMoveEvent().

OR

Instead of using ui.textEdit_log->setMouseTracking(true) use ui.textEdit_log->setAttribute(Qt::WA_TransparentForMouseEvents) this will make your textedit_log invisible for mouse events. In this case you can use the childrenRect() function of your QWidget to determine if your mouse is over the textedit_log.

Chaitanya
  • 177
  • 2
  • 9
1

As far as software design goes, subclassing is a conceptually very heavy operation - it indicates a very deep coupling. It should be used with due concern for the maintainability impact it has. An almost no case should subclassing be used to merely observe the state of an object! - any API that forces you to do that is essentially broken.

Qt provides convenience "event listener" methods, but these methods are not meant to be used as external observers, i.e. you should never override them if all you want is to observe the objectfrom outside. The event filters should be used instead, since they introduce the weakest of possible couplings, and are very generic.

The mouse tracking events will be sent to the widget that tracks the mouse cursor - the editor widget. So no other widget will hear them. You have to implement QObject::filterEvent in a class of your choice (whatever class is convenient), then install that event filter on the editor widget. Your filter will intercept all events sent to the editor, including mouse move events. You should not subclass QTextEdit - use event filters instead!

In any case, the idea to do this via mouse movement tracking is overkill and has some overhead. Instead, look for QEnterEvent and QLeaveEvent - those should be sent to the editor widget without mouse tracking enabled. But still - do not intercept those via any subclassing!

Do note that QWidget is-a QObject, so the event filter can be a widget class, or a non-widget class.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

I solved this problem like this

void Log::enterEvent(QEvent* event)
{
    m_pButton->show();
}

void Log::leaveEvent(QEvent* event)
{
    m_mousePos = this->mapFromGlobal(QCursor::pos());

    QPoint logPos = mapToGlobal(this->normalGeometry().bottomLeft());
    QPoint buttonPos = m_pButton->pos();
    QPoint buttonRelatviePos = buttonPos - logPos;

    // hide button when button is not in the button pos
    if ((m_mousePos.x() < buttonRelatviePos.x()) or (m_mousePos.x() > buttonRelatviePos.x() + m_pButton->width()) or
        (m_mousePos.y() < buttonRelatviePos.y()) or (m_mousePos.y() > buttonRelatviePos.y() + m_pButton->height()))
    {
        m_pButton->hide();
    }
}
jeinne
  • 11
  • 3
  • That's 1/3rd of the way to the correct solution: you found that mouse enter/leave events are there. Next step 2/3: do not subclass, instead install an event filter object on the unadulterated `QTextEdit`. Finally - 3/3 - you'll regret the decision to use `QTextEdit` as a log view - it's not meant for that purpose, and it's very slow when you continually append to it. Instead, you should use e.g. a `QListView` and create your own log data model derived from e.g. `QAbstractListModel`. – Kuba hasn't forgotten Monica Sep 09 '20 at 19:01
  • See: [`QStringList` model example](https://stackoverflow.com/a/38795663/1329652), [and another such example](https://stackoverflow.com/a/18579171/1329652). – Kuba hasn't forgotten Monica Sep 09 '20 at 19:05
  • @UnslanderMonica Sincerely, Thank you for your advise I will try to implement like you are saying. – jeinne Sep 10 '20 at 00:43