我希望QLineEdit
仅在鼠标位于clearbutton
上时显示QLineEdit
(当然字段不为空)。
我已经捕获了分别设置属性的进入和离开事件。除了需要用鼠标手动初始进入和离开QLineEdit
之外,这个工作正常。如何正确启动QLineEdit
,以便从一开始就可以正常工作?
试图模拟最初的鼠标移动没有预期的结果。
cmplLineEdit.h
class cmplLineEdit : public QLineEdit {
Q_OBJECT
public:
explicit cmplLineEdit( QWidget* a_par = 0);
~cmplLineEdit();
private:
void enterEvent( QEvent* a_ev);
void leaveEvent( QEvent* a_ev);
void enableClearButton( bool a_set, int a_del = 0);
private slots:
void initialize( void);
};
cmplLineEdit.cpp
cmplLineEdit::cmplLineEdit( QWidget* a_par) : QLineEdit( a_par) {
m_completeIt = a_cmpl;
setClearButtonEnabled( false);
setFocusPolicy( Qt::StrongFocus);
QTimer::singleShot( 0, this, [=]( void) { initialize(); });
}
cmplLineEdit::~cmplLineEdit() {
}
bool cmplLineEdit::cursorIsInField() {
return rect().contains( mapFromGlobal( QCursor::pos()));
}
void cmplLineEdit::initialize( void) {
QApplication::postEvent( this, new QEvent( ! cursorIsInField() ? QEvent::Enter : QEvent::Leave));
QApplication::postEvent( this, new QEvent( cursorIsInField() ? QEvent::Enter : QEvent::Leave));
}
void cmplLineEdit::enableClearButton( bool a_set, int a_del) {
if( a_del < 0) {
setClearButtonEnabled( a_set);
} else
QTimer::singleShot( a_del, this, [=]( void) { setClearButtonEnabled( a_set); });
}
void cmplLineEdit::enterEvent( QEvent* a_ev) {
enableClearButton( true, 0);
}
void cmplLineEdit::leaveEvent( QEvent* a_ev) {
enableClearButton( false, 0);
}
答案 0 :(得分:0)
是的,鼠标跟踪已开启(否则我无法获得进入和离开事件)。
我重写了代码,自己实现了setClearButtonEnabled()
。现在它正在运作。对于任何感兴趣的人:
cmplEdit.h:
#ifndef CMPLLINEEDIT_H
#define CMPLLINEEDIT_H
#include <QWidget>
#include <QLineEdit>
#include <QCompleter>
#include <QAction>
class cmplLineEdit : public QLineEdit {
Q_OBJECT
public:
explicit cmplLineEdit( bool a_cmpl = true, QWidget* a_par = 0);
~cmplLineEdit();
static QIcon m_icoOff;
static QIcon m_icoOn;
private:
QAction* m_act = nullptr;
bool m_completeIt = true;
void enterEvent( QEvent* a_ev);
void leaveEvent( QEvent* a_ev);
bool cursorIsInField( void);
private slots:
void initialize( void);
void setClearIcon( bool a_set);
void setClearIcon( const QString& a_txt);
};
#endif // CMPLLINEEDIT_H
cmplEdit.cpp:
#include "cmplLineEdit.h"
#include <QTimer>
#include <QDebug>
#include <QStandardItemModel>
#include <QAbstractItemView>
#include <QEvent>
#include <QApplication>
QIcon cmplLineEdit::m_icoOn;
QIcon cmplLineEdit::m_icoOff;
cmplLineEdit::cmplLineEdit( bool a_cmpl, QWidget* a_par) : QLineEdit( a_par) {
if( m_icoOn.isNull()) {
m_icoOn = QIcon( qApp->style()->standardPixmap( QStyle::SP_LineEditClearButton));
}
m_completeIt = a_cmpl;
m_act = addAction( m_icoOn, QLineEdit::ActionPosition::TrailingPosition);
connect( this, SIGNAL( textChanged( QString)), this, SLOT( setClearIcon( QString)));
connect( m_act, SIGNAL( triggered( bool)), this, SLOT( clear()));
QTimer::singleShot( 0, [ this]( void) { initialize(); });
}
cmplLineEdit::~cmplLineEdit() {
}
bool cmplLineEdit::cursorIsInField() {
return rect().contains( mapFromGlobal( QCursor::pos()));
}
void cmplLineEdit::initialize( void) {
setClearIcon( cursorIsInField());
}
void cmplLineEdit::enterEvent( QEvent* a_ev) {
setClearIcon( true);
}
void cmplLineEdit::leaveEvent( QEvent* a_ev) {
setClearIcon( false);
}
void cmplLineEdit::setClearIcon( bool a_set) {
if( m_act == nullptr)
return;
a_set = a_set && ! text().isEmpty();
m_act->setIcon( a_set ? m_icoOn : QIcon());
m_act->setVisible( a_set);
}
void cmplLineEdit::setClearIcon( const QString& a_txt) {
setClearIcon( ! a_txt.isEmpty());
}