0

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();
}
Finder
  • 8,259
  • 8
  • 39
  • 54

1 Answers1

5

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
mavroprovato
  • 8,023
  • 5
  • 37
  • 52
Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
  • I used the these two lines in the mainWidget.cpp this->ui->lineEdit->setFocusProxy(this->ui->lineEdit); this->ui->lineEdit->setFocusPolicy(Qt::StrongFocus); And this line in callSecond() method.. this->ui->lineEdit->setFocus(Qt::OtherFocusReason); . Is it correct ? – Finder Oct 06 '10 at 11:51
  • You didn't understand focusProxy idea. You should set focusProxy to button (so when button gets focus it will automaticly pass it to it's focus proxy). lineEdit by default has StrongFocus, i wrote that you should disable StrongFocus on button. Basicly ui->button->setFocusProxy(ui->lineEdit) should do the thing – Kamil Klimek Oct 06 '10 at 12:09
  • Sorry Kamil. Still I am not getting the ans. I followed the what you suggested.. But I could not get the ans. – Finder Oct 07 '10 at 10:01
  • I've gave you an answer, your button takes focus after you clicked it, that's why your line edit loses focus. You must bring back focus to your line edit – Kamil Klimek Oct 07 '10 at 11:45
  • Hi Kamil , Thanks for your reply. I followed your suggestion. but I can set the focus in the lineEdit when the button and lineedit on the same widget. In my case, the button is present in the second widget and line edit present in the first widget.If I press the button on the second widget, that time i want to set the focus on the line edit on the first widget and hide the second widget . Please help me.. Thanks in advance. – Finder Nov 10 '10 at 12:18