2014-03-02 229 views
1

在我的QWidget中有一些像QLineEdit和QLabels的小工具。我可以很容易地检查我的鼠标是否在QLabel上,是否点击了右键。不像QLineEdit。 我试图继承QLineEdit并重新实现mouseRelease,但它永远不会被调用。 findChild方法是从我的UI中获取相应的小部件。如何检测鼠标点击QLineEdit

我该如何获得鼠标释放并在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); 

点击确认,但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(); 

}