2014-03-02 2 views
1

내 QWidget에는 QLineEdit 및 QLabels와 같은 몇 가지 하위 위젯이 있습니다. 마우스가 QLabel 위에 있고 오른쪽 단추를 클릭했는지 쉽게 확인할 수 있습니다. QLineEdit에서는 그렇지 않습니다. QLineEdit의 하위 클래스를 만들고 mouseRelease를 다시 구현하려고했지만 결코 호출되지 않습니다. findChild-method는 해당 UI를 UI 밖으로 가져 오는 것입니다.QLineEdit에서 마우스 클릭을 감지하는 방법

QLineEdit에서 mouseRelease 및 왼쪽 또는 오른쪽 마우스 버튼을 얻는 방법은 무엇입니까? 라벨에

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

클릭을 인식하지만, QLineEdit에 클릭하면 아래와 같이 아니다 있습니다 : 나는 라인 편집에 클릭을 감지하고 구별 할 수있을 것

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... 
     } 
    } 
+0

가능한 복제본 http://stackoverflow.com/questions/6452077/how-to-get-click-event-of-qlineedit-in-qt – user2672165

+0

나는 이것을 읽었지만 작동하지 않았습니다 ... 그리고 eventFilter'에서 왼쪽 또는 오른쪽 마우스 버튼인지 확인할 수 없습니다 ... – user2366975

+0

그래도 구현에 문제가 있다고 생각되면 유일한 도움을 얻는 방법은 게시하는 것입니다. 물론 관련 코드 만 게시해야합니다. – user2672165

답변

0

가에 입력하는 이는 아래에 게시 된 클래스는

#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

완전성에 대해 언급 한 링크에 게시 된 내용과 매우 유사
#include "QApplication" 

#include "myclass.h" 

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

    MyClass dialog; 
    dialog.show(); 

    return app.exec(); 

} 
관련 문제