2
votes

In my QWidget there are some subwidgets like a QLineEdit and QLabels. I can easily check if my mouse is over a QLabel and if it was clicked on the right button. Not so on QLineEdit. I tried to subclass QLineEdit and re-implement the mouseRelease, but it is never called. The findChild method is to get the corresponding widget out off my UI.

How do I get the mouseRelease and whether it's left or right mouse button in a QLineEdit?

void Q_new_LineEdit::mouseReleaseEvent(QMouseEvent *e){
    qDebug() << "found release";
    QLineEdit::mouseReleaseEvent(e);
}

m_titleEdit = new Q_new_LineEdit();
m_titleEdit = findChild<QLineEdit *>("titleEdit",Qt::FindChildrenRecursively);

Clicks on labels are recognized, but the click on QLineEdit is not, like below:

void GripMenu::mouseReleaseEvent(QMouseEvent *event){

    if (event->button()==Qt::RightButton){ 

        //get click on QLineEdit 
        if (uiGrip->titleEdit->underMouse()){
            //DO STH... But is never called
        }

        //change color of Label ...
        if (uiGrip->col1note->underMouse()){
            //DO STH...
        }
    }
1
I have read this, and it did not work... and in an eventFilter I can't check if it is the left or right mousebutton...user2366975
Ok, but then if you think something is wrong in your implementation the only way to get help is to post it. Of course only the relevant code should be posted.user2672165
Also clicking on QLineEdit is perhaps not so intuitive? I am not saying it is not. I am only saying that you should think of other ways of interaction perhaps.user2672165
Hm that is right. Still I want to know how it could workuser2366975

1 Answers

0
votes

I seem to be able to detect clicks on the line edit and distinguish which type it is in the class posted below which is very similar to what has been posted in the mentioned link

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>
#include <QMouseEvent>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QtCore>


class MyClass: public QDialog
{
  Q_OBJECT
    public:
  MyClass() :
  layout(new QHBoxLayout),
    lineEdit(new QLineEdit)

      {
        layout->addWidget(lineEdit);
        this->setLayout(layout);
        lineEdit->installEventFilter(this);
      }

  bool eventFilter(QObject* object, QEvent* event)
    {
      if(object == lineEdit && event->type() == QEvent::MouseButtonPress) {
        QMouseEvent *k = static_cast<QMouseEvent *> (event);
        if( k->button() == Qt::LeftButton ) {
          qDebug() << "Left click";
        } else if ( k->button() == Qt::RightButton ) {
          qDebug() << "Right click";
        }
      }
      return false;
    }
 private:
  QHBoxLayout *layout;
  QLineEdit *lineEdit;

};


#endif

main.cpp for completeness

#include "QApplication"

#include "myclass.h"

int main(int argc, char** argv)
{
  QApplication app(argc, argv);

  MyClass dialog;
  dialog.show();

  return app.exec();

}