Change focus



  • Can somebody help me!! Why this code is working with QMessageBox::information(0, "js", "no"); but without it doesn't??????
    I want to set focus from lineEdit (numCard) to a pushButton (no) with pressing enter.

    bool Poeni::event(QEvent *event)
    {
    if (event->type() == QEvent::KeyPress)
    {

        QKeyEvent* pKeyEvent=static_cast<QKeyEvent*>(event);
    
        switch(pKeyEvent->key())
        {
    
        case Qt::Key_Return:
        case Qt::Key_Enter:
    

    if(no->hasFocus())
    {

               Otkazi();
                return true;
            }
           else if(numCard->hasFocus() && numCard->text().isEmpty())
           {
    
             QMessageBox::information(0, "js", "no");
              no->setFocus();
               return true;
           }
    

    break;
    }

     }
     return QWidget::event(event);
    

    }



  • Hi,

    Here is what I would try:

    • Use keyPressEvent(QKeyEvent* event) instead of event(QEvent* event) because it's much simpler, and you deal with what you really need to, i.e. key events.
    • Use
    QApplication::processEvents();
    

    instead of

    QMessageBox::information(0, "js", "no");
    
    • Try to setFocus twice in a row (I encountered some weird behavior with focus before).

    Also, could you use code tag in order to format your snippet? It's quite hard to read.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.