Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change focus
Qt 6.11 is out! See what's new in the release blog

Change focus

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suzy Q
    wrote on last edited by
    #1

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

    }

    1 Reply Last reply
    0
    • ValentinMicheletV Offline
      ValentinMicheletV Offline
      ValentinMichelet
      wrote on last edited by ValentinMichelet
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved