Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Focus transmission rejecting
Qt 6.12 LTS is out! See what's new in the release blog

Focus transmission rejecting

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.6k Views 1 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.
  • P Offline
    P Offline
    pr1321
    wrote on last edited by
    #1

    I would like to reject the focus transfer from one widget to another input widget when the data is invalid.
    I tried to process focusOutEvent, but at the same time I can "return" the focus, but not prohibit its transmission.

    I am using qt 4.8.6.

    Is it possible?

    Thanks.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Wouldn't it be simpler to disable whatever cannot be accessed while the prior data are invalid ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        pr1321
        wrote on last edited by pr1321
        #3

        I solved the problem by processing the KeyPressEvent for Key_Tab and Key_BackTab in the event filter and overriding the focusNextPrevChild method of the parent widget.

        bool EditorGroup::eventFilter(QObject * obj, QEvent * event )
        {
            ReportableBase * editor = dynamic_cast<ReportableBase *>(obj);
            if ( event->type() == QEvent::KeyPress )
            {
                QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
                if( editor )
                {
                    if( keyEvent->key() == Qt::Key_Return ||
                            keyEvent->key() == Qt::Key_Tab )
                    {
                        focusNextChild();
                        return true;
                    }
                    else if ( keyEvent->key() == Qt::Key_Backtab )
                    {
                        focusPrev();
                    }
                }
            }
            else if ( event->type() == QEvent::FocusIn )
            {
                if(editor)
                {
                    focusWgt = editor->toWidget();
                }
            }
            return false;
        }
        
        bool EditorGroup::focusNextPrevChild( bool next )
        {
            ReportableBase * editor = dynamic_cast<ReportableBase *>( focusWgt );
            if(editor)
            {
                QString message;
                if(editor->isDataValid(message))
                {
                    ReportableBase * nextEditor = 0;
                    while (nextEditor == 0)
                    {
                        focusWgt = next ?
                                    focusWgt->nextInFocusChain() :
                                    focusWgt->previousInFocusChain();
                        nextEditor = dynamic_cast<ReportableBase *>(focusWgt);
                    }
                    focusWgt->setFocus( next ?
                                            Qt::TabFocusReason :
                                            Qt::BacktabFocusReason );
                    return true;
                }
                else
                {
                    editor->report_warning(message);
                }
            }
            return false;
        }
        
        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
        • Unsolved