QLineEdit Focus Event

此生再无相见时 提交于 2019-12-23 05:27:21

问题


I have two widget mainwindow123 and second-class. In my MainWidget.cpp have one lineedit and button field. Initially I can set the focus on the line edit. But after come from the second.cpp Widget then I could not set the focus on the lineedit. Please help me.. Which place I did the mistake? Thanks in advance.

This is my code MainWidget.cpp

MainWidget::MainWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MainWidget)
    {
        ui->setupUi(this);
        s = new second();
        connect(ui->pushButton, SIGNAL(clicked()),this,SLOT(callSecond()));

    }

    MainWidget::~MainWidget()
    {
        delete ui;
    }
    void MainWidget::callSecond()
    {
       s->show();

     }

second.cpp

second::second(QWidget *parent) :
    QWidget(parent)
{
    QPushButton *first = new QPushButton("first");
    first->setStyleSheet(
         "background-color:black;"

    );
    QGridLayout *d = new QGridLayout();

    d->addWidget(frist,0,0,1,1);
    setLayout(d);
    connect(first,SIGNAL(clicked()),this,SLOT(first()));
}

void second:: first()
{
    this->hide();
}

回答1:


It's because your focus goes to button after you clicked it. You could achieve it by:

  1. Setting a focusProxy http://doc.qt.io/qt-4.8/qwidget.html#setFocusProxy
  2. Disabling strong focus on button: http://doc.qt.io/qt-4.8/qwidget.html#focusPolicy-prop
  3. Connecting buttons clicked signal to setFocus slot of your QLineEdit


来源:https://stackoverflow.com/questions/3869505/qlineedit-focus-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!