在Qt中,可以通过继承QLineEdit类,重载其mousePressEvent()函数实现LineEdit的点击事件。示例代码如下:
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit MyLineEdit(QWidget *parent = nullptr);
protected:
void mousePressEvent(QMouseEvent *event);
};
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent)
{
}
void MyLineEdit::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
// 左键点击事件处理
qDebug() << "left button clicked";
// TODO: 添加你想要执行的操作
}
QLineEdit::mousePressEvent(event);
}
在上述代码中,我们继承了QLineEdit类,并重载了其mousePressEvent()函数,在该函数中处理鼠标左键点击事件,并添加相应的操作。
注意:使用自定义的MyLineEdit类替换原有的QLineEdit控件时,需要将所有相关信号和槽都重新连接一遍。