Cursor still appears in LineEdit although it is not focused any more

588 views Asked by At

I need to combine a LineEdit and a Button together into such an element and I want the lineEdit works as usual. But when I click on the lineEdit, the cursor does not appear, only when I click 3 times, then cursor appears but does not blink. enter image description here

After that I click to another place to lose focus for the lineEdit, I hope the cursor is not there any more but then the cursor is still there.

enter image description here

I know the problem is in my stylesheet but I can not find out where. Could you guys help me?

This is my code:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->lineEdit1->setPlaceholderText("another LineEdit to lose focus for the LineEdit below");
    QHBoxLayout *lineEditWithButtonForm = new QHBoxLayout( this );
    lineEditWithButtonForm->setSpacing(0);
    lineEditWithButtonForm->setContentsMargins(0,0,0,0);
    ui->m_lineEdit->setContentsMargins(10,0,10,0);
    ui->m_lineEdit->setFixedHeight( 25 );
    ui->m_lineEdit->setStyleSheet("QLineEdit{ padding-left: 10px; padding-right: 10px; border-width: 1px 0px 1px 1px; border-style: solid; border-color: rgb(204,204,204); }");
    lineEditWithButtonForm->addWidget(ui->m_lineEdit);
    ui->m_button->setFixedHeight( 25 );
    ui->m_button->setCursor( QCursor( Qt::PointingHandCursor ) );
    ui->m_button->setFocusPolicy(Qt::ClickFocus);
    ui->m_button->setStyleSheet( "QAbstractButton{ border-width: 1px 1px 1px 0px; border-style: solid; border-color: rgb(204,204,204); }");
    lineEditWithButtonForm->addWidget(ui->m_lineEdit);

    ui->m_lineEdit->installEventFilter( this );
}

bool MainWindow::eventFilter( QObject *obj, QEvent *event )
{
    if ( obj == ui->m_lineEdit )
    {
        if ( event->type() == QEvent::FocusIn )
        {
            ui->m_lineEdit->setStyleSheet( "QLineEdit{padding-left: 10px; padding-right: 10px; border-width: 1px 0px 1px 1px; border-style: solid; border-color:rgb(249,125,25)}" );
            ui->m_button->setStyleSheet( "QAbstractButton{ border-width: 1px 1px 1px 0px; border-style: solid; border-color: rgb(249,125,25)}" );
            return true;
        }
        else if ( event->type() == QEvent::FocusOut)
        {
            ui->m_lineEdit->setStyleSheet( "QLineEdit{ padding-left: 10px; padding-right: 10px; border-width: 1px 0px 1px 1px; border-style: solid; border-color: rgb(204,204,204)}" );
            ui->m_button->setStyleSheet( "QAbstractButton{ border-width: 1px 1px 1px 0px; border-style: solid; border-color: rgb(204,204,204) } ");
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return MainWindow::eventFilter( obj, event );
    }
 }
2

There are 2 answers

1
eyllanesc On BEST ANSWER

When you return True in eventFilter() you are preventing the widget where the event was going to be sent from not receiving it, and in this case it is necessary that the FocusIn and FocusOut event is received by the QLineEdit. Using that consideration, the following solution could be proposed:

bool MainWindow::eventFilter( QObject *obj, QEvent *event )
{
    if ( obj == ui->m_lineEdit )
    {
        if ( event->type() == QEvent::FocusIn )
        {
            ui->m_lineEdit->setStyleSheet( "QLineEdit{padding-left: 10px; padding-right: 10px; border-width: 1px 0px 1px 1px; border-style: solid; border-color:rgb(249,125,25)}" );
            ui->m_button->setStyleSheet( "QAbstractButton{ border-width: 1px 1px 1px 0px; border-style: solid; border-color: rgb(249,125,25)}" );
        }
        else if ( event->type() == QEvent::FocusOut)
        {
            ui->m_lineEdit->setStyleSheet( "QLineEdit{ padding-left: 10px; padding-right: 10px; border-width: 1px 0px 1px 1px; border-style: solid; border-color: rgb(204,204,204)}" );
            ui->m_button->setStyleSheet( "QAbstractButton{ border-width: 1px 1px 1px 0px; border-style: solid; border-color: rgb(204,204,204) } ");
        }
    }
    return QMainWindow::eventFilter( obj, event );
}
0
Krishna Prasad P On

FocusIn and FocusOut would be enough for QLineEdit event. Remaining all event need to be handled by QMainWindow. By removing the return false statement in else part could solve the issue.