【问题标题】:Cursor still appears in LineEdit although it is not focused any more光标仍然出现在 LineEdit 中,尽管它不再聚焦
【发布时间】:2018-08-01 18:28:34
【问题描述】:

我需要将 LineEdit 和 Button 组合到这样一个元素中,并且我希望 lineEdit 像往常一样工作。 但是当我点击lineEdit时,光标没有出现,只有当我点击3次时,光标才会出现但不闪烁。

之后我点击另一个地方失去了lineEdit的焦点,我希望光标不再存在但光标仍然存在。

我知道问题出在我的样式表中,但我不知道在哪里。你们能帮帮我吗?

这是我的代码:

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 );
    }
 }

【问题讨论】:

    标签: c++ qt qt5 qlineedit qstylesheet


    【解决方案1】:

    当您在 eventFilter() 中返回 True 时,您正在防止要发送事件的小部件不接收它,在这种情况下,FocusInFocusOut 事件必须由QLineEdit。考虑到这一点,可以提出以下解决方案:

    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 );
    }
    

    【讨论】:

    • 非常感谢您的解释 eyllanesc。祝你今天过得愉快! :)
    【解决方案2】:

    FocusIn 和 FocusOut 对于 QLineEdit 事件就足够了。剩下的所有事件都需要由 QMainWindow 处理。通过删除 else 部分中的 return false 语句可以解决问题。

    【讨论】:

      猜你喜欢
      • 2012-11-30
      • 2018-09-17
      • 2016-04-05
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多