2014-03-02 231 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(); 

}