onmouseover并单击事件QLineEdit

时间:2013-05-17 08:44:42

标签: qt qlineedit

如何为onmouseover定义QLineEdit和点击事件?我想为onmouseover()

制作2个信号clicked()QLineEdit

2 个答案:

答案 0 :(得分:3)

您可以在QLineEdit上安装event filter

以下是一个例子:

QLineEdit *line_edit = new QLineEdit(this);
ui->verticalLayout->addWidget(line_edit);
line_edit->installEventFilter(this);

在您的事件过滤器功能中,您可以执行以下操作:(这是您重写的功能)

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        qDebug() << "CLICK";
    }
    if(event->type() == QEvent::MouseMove)
    {
        qDebug() << "MOUSE OVER";
    }
    return false;
}

答案 1 :(得分:1)

我担心,你必须继承QLineEdit并覆盖void mouseMoveEvent ( QMouseEvent * event )void mousePressEvent ( QMouseEvent * event )(或void mouseReleaseEvent ( QMouseEvent * event ),如果你愿意的话)。当没有按住鼠标按钮时,不要忘记调用setMouseTracking(true);来跟踪鼠标移动。