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. Key press (arrow keys) cause loss of focus

Key press (arrow keys) cause loss of focus

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 5 Posters 5.2k Views
  • 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 Perdrix
    28 Dec 2023, 09:25

    @Axel-Spoerl For further information this is what the code I use does as the image starts to load and on completion. All the controls referenced here are in the "central widget" the main window, not the dock widget.

    Starting to load:

    //
    // Display the "Loading filename" with red background gradient while loading in background
    //
    ui->information->setStyleSheet(
    	"QLabel { background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,"
    	"stop:0 rgba(252, 251, 222, 0), stop:1 rgba(255, 151, 154, 255)) }");
    ui->information->setText(tr("Loading %1", "IDS_LOADPICTURE")
    	.arg(fileName));
    //
    // No longer interested in signals from the imageView object
    //
    ui->picture->disconnect(editStars, nullptr);
    ui->picture->disconnect(selectRect, nullptr);
    
    pToolBar->setVisible(false); pToolBar->setEnabled(false);
    editStars->setBitmap(nullptr);
    

    When the image completes loading:

    //
    // The image we want is available in the cache
    //
    m_LoadedImage.m_Image = pImage;
    m_LoadedImage.m_pBitmap = pBitmap;
    if (m_GammaTransformation.isInitialized())
        ApplyGammaTransformation(m_LoadedImage.m_Image.get(), m_LoadedImage.m_pBitmap.get(), m_GammaTransformation);
    ui->picture->setPixmap(QPixmap::fromImage(*(m_LoadedImage.m_Image)));
    
    if (frameList.isLightFrame(fileToShow))
    {
    	editStars->setLightFrame(fileName);
    	editStars->setBitmap(pBitmap);
    	if (pToolBar->rectAction->isChecked())
    	{
    		editStars->rectButtonPressed();
    		selectRect->rectButtonPressed();
    	}
    	else if (pToolBar->starsAction->isChecked())
    	{
    		editStars->starsButtonPressed();
    		selectRect->starsButtonPressed();
    	}
    	else if (pToolBar->cometAction->isChecked())
    	{
    		editStars->cometButtonPressed();
    		selectRect->cometButtonPressed();
    	}
    	pToolBar->setVisible(true); pToolBar->setEnabled(true);
    }
    else
    {
    	pToolBar->setVisible(false); pToolBar->setEnabled(false);
    	editStars->setBitmap(nullptr);
    };
    
    CBilinearParameters		Transformation;
    VOTINGPAIRVECTOR		vVotedPairs;
    if (frameList.getTransformation(fileName, Transformation, vVotedPairs))
    					editStars->setTransformation(Transformation, vVotedPairs);
    ui->information->setStyleSheet(
        "QLabel { background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,"
        "stop:0 rgba(138, 185, 242, 0), stop:1 rgba(138, 185, 242, 255)) }");
    ui->information->setText(fileName);
    
    

    The loss of focus on the table view happens just after the code for completion of loading is finished (IOW the table view retains focus and selection until just after loading has finished).

    A Offline
    A Offline
    Axel Spoerl
    Moderators
    wrote on 28 Dec 2023, 14:18 last edited by
    #15

    Hi @Perdrix,
    happy Christmas to you to and all the best for upcoming 2024!

    Sorry for asking the dock widget stuff. My initial thought was that you are suffering from a long standing bug, where a manually undocked dock widget may end up wrapped in an invisible QDockWidgetGroupWindow. Besides crashing when hovered over another floating dock, it is well known for mangling the focus chain. But that doesn't seem to be the case here. Just btw, this (and a whole bunch of other fixes) make dock widgets pretty stable from 6.5 onward.

    I can't figure anything fishy in the code.

    I downloaded and inspected the Excel file. Thanks for the explanations thereof. However, it doesn't seem to contain qDebug() << QApplication::focusWidget(); and qDebug() << targetWidget::focusWidget(). Would be great if you could add those into the diagnostic output. The former will tell us, when the table view has lost focus. The latter will tell us, if a focus or a broken chain has gotten in our way.

    Cheers
    Axel

    Software Engineer
    The Qt Company, Oslo

    P 1 Reply Last reply 28 Dec 2023, 15:46
    0
    • A Axel Spoerl
      28 Dec 2023, 14:18

      Hi @Perdrix,
      happy Christmas to you to and all the best for upcoming 2024!

      Sorry for asking the dock widget stuff. My initial thought was that you are suffering from a long standing bug, where a manually undocked dock widget may end up wrapped in an invisible QDockWidgetGroupWindow. Besides crashing when hovered over another floating dock, it is well known for mangling the focus chain. But that doesn't seem to be the case here. Just btw, this (and a whole bunch of other fixes) make dock widgets pretty stable from 6.5 onward.

      I can't figure anything fishy in the code.

      I downloaded and inspected the Excel file. Thanks for the explanations thereof. However, it doesn't seem to contain qDebug() << QApplication::focusWidget(); and qDebug() << targetWidget::focusWidget(). Would be great if you could add those into the diagnostic output. The former will tell us, when the table view has lost focus. The latter will tell us, if a focus or a broken chain has gotten in our way.

      Cheers
      Axel

      P Offline
      P Offline
      Perdrix
      wrote on 28 Dec 2023, 15:46 last edited by
      #16

      @Axel-Spoerl said in Key press (arrow keys) cause loss of focus:

      I downloaded and inspected the Excel file. Thanks for the explanations thereof. However, it doesn't seem to contain qDebug() << QApplication::focusWidget(); and qDebug() << targetWidget::focusWidget(). Would be great if you could add those into the diagnostic output. The former will tell us, when the table view has lost focus. The latter will tell us, if a focus or a broken chain has gotten in our way.

      I'm puzzled too , the focus change events all have a details section containing the reason for the focus change, then the class name QApplication::focusWidget() and then class name of myTableView->focusWidget():

      e.g.: Window System;QScrollArea 0;QTableView 0

      I've modified the code to use a QDebug object to display the object pointer returned by focusWidget() calls.

      The updated xls file is here:

      www.perdrix.co.uk/DSSEvents%202023-12-28T15-41-51.xlsx

      David

      A 1 Reply Last reply 28 Dec 2023, 19:48
      0
      • P Perdrix
        28 Dec 2023, 15:46

        @Axel-Spoerl said in Key press (arrow keys) cause loss of focus:

        I downloaded and inspected the Excel file. Thanks for the explanations thereof. However, it doesn't seem to contain qDebug() << QApplication::focusWidget(); and qDebug() << targetWidget::focusWidget(). Would be great if you could add those into the diagnostic output. The former will tell us, when the table view has lost focus. The latter will tell us, if a focus or a broken chain has gotten in our way.

        I'm puzzled too , the focus change events all have a details section containing the reason for the focus change, then the class name QApplication::focusWidget() and then class name of myTableView->focusWidget():

        e.g.: Window System;QScrollArea 0;QTableView 0

        I've modified the code to use a QDebug object to display the object pointer returned by focusWidget() calls.

        The updated xls file is here:

        www.perdrix.co.uk/DSSEvents%202023-12-28T15-41-51.xlsx

        David

        A Offline
        A Offline
        Axel Spoerl
        Moderators
        wrote on 28 Dec 2023, 19:48 last edited by
        #17

        @Perdrix
        Hi David,

        hm, that looks like the table view consumes a straight forward QEvent::FocusOut. That must originate from somewhere.
        To figure that out, I'd set a break point on the focus out event handler and look at the call stack. Someone is stealing focus here. Much grief is caused by focus thieves.

        Cheers
        Axel

        Software Engineer
        The Qt Company, Oslo

        P 1 Reply Last reply 29 Dec 2023, 11:01
        1
        • A Axel Spoerl
          28 Dec 2023, 19:48

          @Perdrix
          Hi David,

          hm, that looks like the table view consumes a straight forward QEvent::FocusOut. That must originate from somewhere.
          To figure that out, I'd set a break point on the focus out event handler and look at the call stack. Someone is stealing focus here. Much grief is caused by focus thieves.

          Cheers
          Axel

          P Offline
          P Offline
          Perdrix
          wrote on 29 Dec 2023, 11:01 last edited by
          #18

          @Axel-Spoerl QT version 6.6.1: Here's the call stack at the time the event filter receives that FocusOut event:

          336bd850-a707-46ff-a1e8-10dccc5a3152-image.png

          It's not at all clear to me why it is doing that.

          If there's information you need me to dig out from that call stack please let me know.

          David

          A 1 Reply Last reply 29 Dec 2023, 12:39
          0
          • P Perdrix
            29 Dec 2023, 11:01

            @Axel-Spoerl QT version 6.6.1: Here's the call stack at the time the event filter receives that FocusOut event:

            336bd850-a707-46ff-a1e8-10dccc5a3152-image.png

            It's not at all clear to me why it is doing that.

            If there's information you need me to dig out from that call stack please let me know.

            David

            A Offline
            A Offline
            Axel Spoerl
            Moderators
            wrote on 29 Dec 2023, 12:39 last edited by
            #19

            @Perdrix
            Hi David,

            the gui event dispatcher sends posted events, which makes it forward window system events. The first of those causes a focus change. It doesn't result from the application's main thread, because we don't see any postEvent()in the call stack. AFAIK, that can have two possible reasons:

            • The window manager decides, that another application gets focus. It can be anything from the debugger to a system popup or a logging window receiving output to display.
            • The focus widget disappears (it gets hidden or destroyed) and the focus chain is broken, which is why the next focus widget can't be established.

            It would be helpful to know the values of QWidget * focus, Qt::FocusReason reasonin Line 1539 - 6 lines down from the break point. The widget will probably be nullptr, because that's where we end up. The focus reason will not tell us what exactly is going on, but it will tell us why the focus was changed.

            Software Engineer
            The Qt Company, Oslo

            P 1 Reply Last reply 29 Dec 2023, 14:53
            0
            • A Axel Spoerl
              29 Dec 2023, 12:39

              @Perdrix
              Hi David,

              the gui event dispatcher sends posted events, which makes it forward window system events. The first of those causes a focus change. It doesn't result from the application's main thread, because we don't see any postEvent()in the call stack. AFAIK, that can have two possible reasons:

              • The window manager decides, that another application gets focus. It can be anything from the debugger to a system popup or a logging window receiving output to display.
              • The focus widget disappears (it gets hidden or destroyed) and the focus chain is broken, which is why the next focus widget can't be established.

              It would be helpful to know the values of QWidget * focus, Qt::FocusReason reasonin Line 1539 - 6 lines down from the break point. The widget will probably be nullptr, because that's where we end up. The focus reason will not tell us what exactly is going on, but it will tell us why the focus was changed.

              P Offline
              P Offline
              Perdrix
              wrote on 29 Dec 2023, 14:53 last edited by
              #20

              @Axel-Spoerl I put this in my Event Logging code:

                  case QEvent::FocusOut:
                      inputType = FOCUS;
                      eventType = "FocusOut";
                      if (tableView == obj) __debugbreak();
                      break;
              

              When the breakpoint is hit I think the relevant part of the call stack is:

              b75a63ec-94b8-446d-a1c8-15b897cfdae4-image.png

              where the focus change is being driven from QGuiApplicationPrivate::processActivatedEvent() at line 2562. At that point in the code, newFocus is a null pointer and previousFocusObject -> my table view object.

              So why is the code forcing a focus change?

              David

              A 1 Reply Last reply 29 Dec 2023, 15:10
              0
              • P Perdrix
                29 Dec 2023, 14:53

                @Axel-Spoerl I put this in my Event Logging code:

                    case QEvent::FocusOut:
                        inputType = FOCUS;
                        eventType = "FocusOut";
                        if (tableView == obj) __debugbreak();
                        break;
                

                When the breakpoint is hit I think the relevant part of the call stack is:

                b75a63ec-94b8-446d-a1c8-15b897cfdae4-image.png

                where the focus change is being driven from QGuiApplicationPrivate::processActivatedEvent() at line 2562. At that point in the code, newFocus is a null pointer and previousFocusObject -> my table view object.

                So why is the code forcing a focus change?

                David

                A Offline
                A Offline
                Axel Spoerl
                Moderators
                wrote on 29 Dec 2023, 15:10 last edited by
                #21

                @Perdrix
                That baffles me. Can you step into Qt code?
                Or isolate the issue in a minimal reproducer?

                Software Engineer
                The Qt Company, Oslo

                P 1 Reply Last reply 29 Dec 2023, 15:34
                0
                • A Axel Spoerl
                  29 Dec 2023, 15:10

                  @Perdrix
                  That baffles me. Can you step into Qt code?
                  Or isolate the issue in a minimal reproducer?

                  P Offline
                  P Offline
                  Perdrix
                  wrote on 29 Dec 2023, 15:34 last edited by Perdrix 1 Feb 2024, 10:24
                  #22

                  @Axel-Spoerl I can step into and put breakpoints into the Qt code.

                  Please tell me where you want the breakpoints and what information you need

                  A 1 Reply Last reply 2 Jan 2024, 10:37
                  0
                  • P Perdrix
                    29 Dec 2023, 15:34

                    @Axel-Spoerl I can step into and put breakpoints into the Qt code.

                    Please tell me where you want the breakpoints and what information you need

                    A Offline
                    A Offline
                    Axel Spoerl
                    Moderators
                    wrote on 2 Jan 2024, 10:37 last edited by
                    #23

                    @Perdrix
                    If you can compile Qt, throw a qDebug() << __FUNCTION__ << typeinto the c'tor of QFocusEvent (qevent.cpp:1562).
                    Set a break point there and continue, unless it's a QEvent::FocusOut. If it's the FocusOut, that eventually steals focus, the call stack will tell who it is.

                    Software Engineer
                    The Qt Company, Oslo

                    P 1 Reply Last reply 2 Jan 2024, 12:01
                    0
                    • A Axel Spoerl
                      2 Jan 2024, 10:37

                      @Perdrix
                      If you can compile Qt, throw a qDebug() << __FUNCTION__ << typeinto the c'tor of QFocusEvent (qevent.cpp:1562).
                      Set a break point there and continue, unless it's a QEvent::FocusOut. If it's the FocusOut, that eventually steals focus, the call stack will tell who it is.

                      P Offline
                      P Offline
                      Perdrix
                      wrote on 2 Jan 2024, 12:01 last edited by
                      #24

                      @Axel-Spoerl :

                      d9316882-fba3-4b20-b1df-8b73ae72d369-image.png

                      The reason in the focus event is 3 (Qt::ActiveWindowFocusReason).

                      This was invoked indirectly from line 1940 in QApplicationPrivate::notifyActiveWindowChange() 239a6261-53c2-480c-aca5-36ff2ed765a2-image.png

                      David

                      A 1 Reply Last reply 2 Jan 2024, 14:26
                      0
                      • P Perdrix
                        2 Jan 2024, 12:01

                        @Axel-Spoerl :

                        d9316882-fba3-4b20-b1df-8b73ae72d369-image.png

                        The reason in the focus event is 3 (Qt::ActiveWindowFocusReason).

                        This was invoked indirectly from line 1940 in QApplicationPrivate::notifyActiveWindowChange() 239a6261-53c2-480c-aca5-36ff2ed765a2-image.png

                        David

                        A Offline
                        A Offline
                        Axel Spoerl
                        Moderators
                        wrote on 2 Jan 2024, 14:26 last edited by
                        #25

                        @Perdrix said in Key press (arrow keys) cause loss of focus:

                        The reason in the focus event is 3 (Qt::ActiveWindowFocusReason).

                        That's interesting. What's the QWidget *focus argument pointing to?
                        Shouldn't be nullptr with ActiveWindowFocusReason...

                        Software Engineer
                        The Qt Company, Oslo

                        P 1 Reply Last reply 2 Jan 2024, 15:28
                        0
                        • A Axel Spoerl
                          2 Jan 2024, 14:26

                          @Perdrix said in Key press (arrow keys) cause loss of focus:

                          The reason in the focus event is 3 (Qt::ActiveWindowFocusReason).

                          That's interesting. What's the QWidget *focus argument pointing to?
                          Shouldn't be nullptr with ActiveWindowFocusReason...

                          P Offline
                          P Offline
                          Perdrix
                          wrote on 2 Jan 2024, 15:28 last edited by Perdrix 1 Feb 2024, 15:33
                          #26

                          @Axel-Spoerl It's pointing to a QScrollArea object (which could be the scroll area in the other dock widget)

                          A 1 Reply Last reply 2 Jan 2024, 16:12
                          0
                          • P Perdrix
                            2 Jan 2024, 15:28

                            @Axel-Spoerl It's pointing to a QScrollArea object (which could be the scroll area in the other dock widget)

                            A Offline
                            A Offline
                            Axel Spoerl
                            Moderators
                            wrote on 2 Jan 2024, 16:12 last edited by
                            #27

                            @Perdrix
                            Getting more and more interesting.
                            Could you assign a QObject::objectName()to the suspicious scroll area? That will become visible in the debugger, so you can easily identify which one it is. Looks like the arrow key event gets delivered to another dock widget, which then consumes the event in an attempt to scroll. Eventually it gets focus as a stray bullet.

                            Software Engineer
                            The Qt Company, Oslo

                            P 1 Reply Last reply 3 Jan 2024, 00:52
                            0
                            • A Axel Spoerl
                              2 Jan 2024, 16:12

                              @Perdrix
                              Getting more and more interesting.
                              Could you assign a QObject::objectName()to the suspicious scroll area? That will become visible in the debugger, so you can easily identify which one it is. Looks like the arrow key event gets delivered to another dock widget, which then consumes the event in an attempt to scroll. Eventually it gets focus as a stray bullet.

                              P Offline
                              P Offline
                              Perdrix
                              wrote on 3 Jan 2024, 00:52 last edited by
                              #28

                              @Axel-Spoerl The variable focusWidget set at line 1937 in QApplicationPrivate::notifyActiveWindowChange() points to the QMainWindow derived class for the application. The code then calls setActiveWindow() for that.

                              By the time this all gets to the actual Event Filter code the object to which focus is being given is the scroll area that belongs to the "other" dock widget.

                              I can recreate the problem without use of arrow keys - just clicking on a row in the table view selects it and starts the loading of the image. Just after the loading of the image completes, the focus is lost. The code that executes in my application on completion of loading the image was posted earlier in this thread.

                              P 1 Reply Last reply 3 Jan 2024, 02:26
                              0
                              • P Perdrix
                                3 Jan 2024, 00:52

                                @Axel-Spoerl The variable focusWidget set at line 1937 in QApplicationPrivate::notifyActiveWindowChange() points to the QMainWindow derived class for the application. The code then calls setActiveWindow() for that.

                                By the time this all gets to the actual Event Filter code the object to which focus is being given is the scroll area that belongs to the "other" dock widget.

                                I can recreate the problem without use of arrow keys - just clicking on a row in the table view selects it and starts the loading of the image. Just after the loading of the image completes, the focus is lost. The code that executes in my application on completion of loading the image was posted earlier in this thread.

                                P Offline
                                P Offline
                                Perdrix
                                wrote on 3 Jan 2024, 02:26 last edited by
                                #29

                                @Axel-Spoerl I found the problem!!!

                                The code that was called on completion of loading of the image contained the following:

                                if (pToolBar->rectAction->isChecked())
                                {
                                	editStars->rectButtonPressed();
                                	selectRect->rectButtonPressed();
                                }
                                else if (pToolBar->starsAction->isChecked())
                                {
                                	editStars->starsButtonPressed();
                                	selectRect->starsButtonPressed();
                                }
                                else if (pToolBar->cometAction->isChecked())
                                {
                                	editStars->cometButtonPressed();
                                	selectRect->cometButtonPressed();
                                }
                                

                                each of the xxxxButtonPressed() member functions called activateWindow(); which in the end resulted in the table view losing focus. I removed the activateWindow(); calls and now it works as it should.

                                Thank you so much for your help - without it I would probably still be struggling with this for many weeks to come.

                                A 1 Reply Last reply 3 Jan 2024, 05:23
                                1
                                • P Perdrix has marked this topic as solved on 3 Jan 2024, 02:26
                                • P Perdrix
                                  3 Jan 2024, 02:26

                                  @Axel-Spoerl I found the problem!!!

                                  The code that was called on completion of loading of the image contained the following:

                                  if (pToolBar->rectAction->isChecked())
                                  {
                                  	editStars->rectButtonPressed();
                                  	selectRect->rectButtonPressed();
                                  }
                                  else if (pToolBar->starsAction->isChecked())
                                  {
                                  	editStars->starsButtonPressed();
                                  	selectRect->starsButtonPressed();
                                  }
                                  else if (pToolBar->cometAction->isChecked())
                                  {
                                  	editStars->cometButtonPressed();
                                  	selectRect->cometButtonPressed();
                                  }
                                  

                                  each of the xxxxButtonPressed() member functions called activateWindow(); which in the end resulted in the table view losing focus. I removed the activateWindow(); calls and now it works as it should.

                                  Thank you so much for your help - without it I would probably still be struggling with this for many weeks to come.

                                  A Offline
                                  A Offline
                                  Axel Spoerl
                                  Moderators
                                  wrote on 3 Jan 2024, 05:23 last edited by
                                  #30

                                  @Perdrix
                                  Hi David,
                                  glad that the issue is resolved!
                                  Thanks for letting me know - was a pleasure.
                                  Cheers
                                  Axel

                                  Software Engineer
                                  The Qt Company, Oslo

                                  1 Reply Last reply
                                  0

                                  24/30

                                  2 Jan 2024, 12:01

                                  • Login

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